Hello,
I’m trying to set up two tables in a database and then make two queries to the posts table. The first query is supposed to return the first five blog posts, and the second is supposed to return all the comments for the blog posts where the user id equals 1.
[php]
<?php include("dbconnect.php"); // using dbconnect object $con = new dbconnect(); $con->connect(); mysql_query("CREATE table posts ( post_id INT NOT NULL AUTO_INCREMENT Primary Key, author VARCHAR(32) NOT NULL, title VARCHAR(32) NOT NULL, content VARCHAR(32) NOT NULL, date_published TIMESTAMP(8))") or die(mysql_error()); mysql_query("CREATE table comments (comment_id INT NOT NULL AUTO_INCREMENT Primary Key, post_id INT NOT NULL, name VARCHAR(32) NOT NULL, email VARCHAR(32) NOT NULL, website VARCHAR(32) NOT NULL, content TEXT NOT NULL, date_published TIMESTAMP(8))") or die(mysql_error()); mysql_query("INSERT INTO posts (post_id, author, title, content) VALUES(1, 'John', 'First post', 'Lorem ipsum dolor sit amet'), (2, 'Tom', 'Second post', 'Sed ullamcorper consectetur ligula'), (3, 'Jane', 'Third post', 'Art'), (4, 'Paul', 'Fourth post', 'Sed ullamcorper consectetur ligula'), (5, 'clark', 'Fifth post', 'Aliquam velit nunc, accumsan id semper a'), (6, 'Mary', 'Sixth post', 'habitant morbi tristique senectus ') or die(mysql_error()"); mysql_query("INSERT INTO comments (comment_id, post_id, name, email, website, content) VALUES(1, 3, 'Tom', '[email protected]', 'www.google.com', 'Pellentesque habitant morbi tristique senectus'), (2, 1, 'Peter', '[email protected]', 'www.yahoo.com', 'convallis et ipsum. Integer'), (3, 5, 'Amanda', '[email protected]', 'www.metacritic.com', 'accumsan id semper a,'), (4, 1, 'Clark', '[email protected]', 'www.asu.edu', 'ipsum neque et nulla'), (5, 4, 'Paul', '[email protected]', 'www.firefox.com', 'Duis elit nisl, laoreet at fringilla'), (6, 2, 'Steve', '[email protected]', 'www.kudzu.com', 'a, convallis et ipsum. ') or die(mysql_error()"); $result = mysql_query("SELECT post_id, author, content FROM posts WHERE id<6"); print(""); print("Id | "); print("Author | "); print("Title | "); print("Content | "); print("
" . $row['id'] . " | " . $row['author'] . " | " . $row['title'] . " | " . $row['content'] . " | "; print("
"; } mysql_close($con); ?>
[/php]
The dbconnect class contains:
[php]<?php
class dbconnect{
function connect()
{
$con=mysql_connect(“localhost”,“username",“password");
if (!$con)
{ die('Could not connect: ’ . mysql_error()); }
mysql_select_db(“username”, $con); // your phpMySQL user name
}
} ?> [/php]
This was a homework assignment for a course, but I already turned it in and lost points since it didn’t work. I’m trying to figure out what I did wrong for my next assignment. All I get when I try to access the page with this script is the phrase “table already created”, instead of any query results. Any help would be greatly appreciated.