|
-
Sep 24th, 2009, 05:28 AM
#1
Re: Creating databases or tables for images?
 Originally Posted by kows
oh, and I wouldn't suggest using any of the code that NightHawk posted. it's messy and has a lot of repetitive code, and the only feature of it is that it has an INSERT query at the end of it. you should not get into the habit of echoing HTML, as PHP is an embedded language and should be treated like one.
I should point out that the code I posted above is code from my php class last year and we were taught to code like that. However, the code we were give this year does not use html within the echo statement but like so:
PHP Code:
echo 'authenticated=queryFailed';
The above just being the echo then message in single quotes!
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Sep 24th, 2009, 07:53 AM
#2
Thread Starter
Addicted Member
Re: Creating databases or tables for images?
Hello,
Thanks all. I have tried your suggestions and worked with the code you posted. I have posted below what I have made out of it so far.
PHP Code:
<?php
//the connection details to mysql
$username = "root";
$password = "";
$hostname = "localhost";
//making the real connection to the database
$dbhandle = mysql_connect ($hostname, $username, $password)
or die ("I am unable to connect: " . mysql_error());
echo "Connected to MYSQL <br />";
?>
<?php
//select a database to work with
$selected = mysql_select_db("StudentEssay", $dbhandle)
or die("Could not select StudentEssay");
?>
<?php
//create the articles table
$sql = "CREATE TABLE IF NOT EXISTS articles(
article_id INT(4) NOT NULL AUTO_INCREMENT,
article_writer VARCHAR(20) NOT NULL,
article_title VARCHAR(100) NOT NULL,
article_intro VARCHAR(220) NOT NULL,
article_body LONGTEXT NOT NULL,
article_date DATE NOT NULL,
PRIMARY KEY (article_id)
)";
$results = mysql_query($sql)
or die (mysql_error());
echo "article table successfully created.";
//the code below is one of the two articles that I have inserted
//into the articles table through phpmyadmin
/***
INSERT INTO `studentessay`.`articles` (`article_id`, `article_writer`,
`article_title`, `article_intro`, `article_body`, `article_date`) VALUES
(NULL, 'Sammy King', 'If you were to write an essay on a web scripting
language, what would it be? PHP or JavaScript?', 'The popularity of PHP is
making it the scripting language for all. Most dynamic websites today rely
heavily on it.', 'Ever since the release of PHP in the early 1990s, the power
and uniqueness of the language has been obvious to all.
', NULL);
*******/
echo "<img src='full_time_students/images/sammy.jpg' />";
?>
<?php
//our query. we want the last 5 articles.
$sql = "SELECT * FROM articles ORDER BY article_date DESC LIMIT 5";
$query = mysql_query($sql);
while($articles = mysql_fetch_assoc($query)){
//we now have an associative array stored in $article of our records.
//dump all of the data
print_r($articles);
//we can refer to these values like so:
// $article['article_id'] => the article id
// $article['article_intro'] => the article introduction
echo "Article #{$articles['article_id']} was posted on {$articles
['article_date']}!";
}
?>
When I run the code above, I get the output below.
HTML Code:
Array ( [article_id] => 2 [article_writer] => Eva Dickenson [article_title] => The Beauty, the Wonder and the Joy of PHP and MYSQL [article_intro] => Many web programmers have come to accept PHP and MYSQL as the ulimate tools for creating dynamic web pages. [article_body] => For some years now, web developers have been using both PHP and MYSQl to create dynamic and database-driven websites. [article_date] => 2009-09-24 ) Article #2 was posted on 2009-09-24!Array ( [article_id] => 1 [article_writer] => Sammy King [article_title] => If you were to write an essay on a web scripting language, what would it be? PHP or JavaScript? [article_intro] => The popularity of PHP is making it the scripting language for all. Most dynamic websites today rely heavily on it. [article_body] => Ever since the release of PHP in the early 1990s, the power and uniqueness of the language has been obvious to all. [article_date] => 2009-09-23 ) Article #1 was posted on 2009-09-23!
But I have one more question please. Why am I getting these pointers such as: [article_intro] =>?
if you need help building a query in the first place, you could take a look at this post that I made quite a while ago.
I have read and saved that post somewhere for future reference. It was really useful and I am going to read it all again.
don't see a place for you to store your actual article, though. it should be a text field, and text fields have no limit to the amount of data they can store.
I have now added a field for the full article (article_body and I set it to LONGTEXT) and you can see it from the code. I hope I have not done it wrongly.
Thanks.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|