Hello, everyone. I’m quite a beginner at php, and I may be mixing legacy code, so do please point out all the problems in my code.
I have this problem of not getting my database. I am trying to create my own blog. However, I am unable to pull up the blog posts, for I get the “No database selected” error.
I’ve searched the internet, but most fixes came from adding the mysql_select_db(), which I already have. I added in mysql_error() after every function call to see where the error came from. The exact error is: “Error in query (3): No database selected.” This comes from the else{} function in GetBlogPosts{}, which is near the end. I’m at a total loss on how to resolve this. I’ve also checked and rechecked my privileges for the account. I’ve deleted and remade them, too.
[php]
<?php include('blogpost.php'); /* adds the database connection mysql_connect("localhost_name", "username", "password") */ $con = mysql_connect("127.0.0.1", "root", ""); if (!$con) { die ('Could not connect to server: ' . mysql_error()); } mysql_select_db('bento-blog', $con) or die ('Could not connect to database: ' . mysql_error()); //now trying to retrieve the blog posts from the database function GetBlogPosts($inId=null, $inTagId=null) { //asking if each argument is empty or null if (!empty($inId)) { //give blog ID, want just this single blog post $sql = "SELECT * FROM blog_posts WHERE id = " . $inId . " ORDER BY id DESC"; $query = mysql_query($sql); if (!$query) { die('Error in query (1) : ' . mysql_error()); } } else if (!empty($inTagId)) { //give tag ID, want all posts with this tag $sql = "SELECT blog_posts.* FROM blog_post_tags LEFT JOIN (blog_posts) ON (blog_post_tags.postID = blog_posts.id) WHERE blog_post_tags.tagID =" . $tagID . " ORDER BY blog_posts.id DESC"; $query = mysql_query($sql); if (!$query) { die('Error in query (2) : ' . mysql_error()); } } else { //otherwise want all blog posts $sql = "SELECT * FROM blog_posts ORDER BY id DESC"; $query = mysql_query($sql); if (!$query) { die('Error in query (3) : ' . mysql_error()); } } //process data returned from query, create object, and create an array $postArray = array(); while ($row = mysql_fetch_assoc($query)) { $myPost = new BlogPost($row["id"], $row['title'], $row['post'], $row['author'], $row['datePosted']); array_push($postArray, $myPost); } return $postArray; } mysql_close($con); ?>[/php]
Thank you for any assistance you can offer.