No Database Selected

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.

Hi,

Can you tell me from where this function ‘GetBlogPosts’ called?

Or

can you post the “blogpost.php” file here.

Hi

you ca try this

[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;
}
include(‘blogpost.php’);
mysql_close($con);

[/php]

The only difference I could tell between the code you gave me from my original code is that the

[php]include(‘blogpost.php’)[/php]

was at the bottom before the mysql_close() function.

I put in your code, and got a slew of errors. :frowning:

This is blogpost.php

[php]<?php

class BlogPost
{
var $id;
var $title;
var $author;
var $tags;
var $datePosted;

function __construct($inId=null, $inTitle=null, $inPost=null, $inAuthorId=null, $inDatePosted=null)
{
	//all the if statements to make sure not pass nothing to server, which produces error
	if (!empty($inId))
	{
		$this->id = $inId;
	}
	if (!empty($inTitle))
	{
		$this->title = $inTitle;
	}
	if (!empty($inPost))
	{
		$this->post = $inPost;
	}
	if (!empty($inDatePosted))
	{
		//to get date in dd/mm/yyyy format
		// explode() is remove the components of the date
		$splitDate = explode("-", $inDatePosted);
		$this->datePosted = $splitDate[2] . "/" . $splitDate[1] . "/" . $splitDate[0];
	}
	if (!empty($inAuthorId))
	{
		//to get the author's name using a query
		$query = mysql_query("SELECT poster_name FROM People WHERE id = " . $inAuthorId);
		$row = mysql_fetch_assoc($query);
		$this->author = $row["poster_name"];
	}
	
	//setting default, will be overwritten if tag database not empty
	$postTags = "No Tags";
	if (!empty($inId))
	{
		//performing a LEFT JOIN
		//this selects data from another table but only when it matches the data from the "left" (the other selected data) 
		$sql = "SELECT tags.* LEFT JOIN (tags) ON (blog_post_tags.tag_id = tags.id) WHERE blog_post_tags.blog_post_id = " . $inId;
		$query = mysql_query($sql);
		$tagArray = array();
		$tagIDArray = array();
		while($row = mysql_fetch_assoc($query))
		{
			array_push($tagArray, $row["name"]);
			array_push($tagIDArray, $row["id"]);
		}
		//check to make sure array has length>0 with if-else
		if (sizeof($tagArray) > 0)
		{
			foreach ($tagArray as $tag)
			{
				if ($postTags == "No Tags")
				{
					$postTags = $tag;
				}
				else
				{
					$postTags = $postTags . ", " . $tag;
				}
			}
		}
	}	
	$this->tags = $postTags;
}

}

?>[/php]

I’m calling it from index.php from the content div.

[code]


<?php
include (“includes/includes.php”);
	$blogPosts = GetBlogPosts($inId=null, $inTagId=null);
	
	foreach ($blogPosts as $post)
	{
		echo "<div class='post'>";
		echo "<h2>" . $post->title . "</h2>";
		echo "<p>" . $post->post . "</p";
		echo "<span class='footer'>Posted By: " . $post->author . " Posted On: " . $post->datePosted . " Tags: " . $post->tags . "</span";
		echo "</div>";
	}
?>
</div>

[/code]

Hi,

here is he solution.

[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]

And

[php]

<?php include ("includes/includes.php"); $blogPosts = GetBlogPosts($inId, $inTagId); foreach ($blogPosts as $post) { echo "
"; echo "

" . $post->title . "

"; echo "

" . $post->post . "</p"; echo "Posted By: " . $post->author . " Posted On: " . $post->datePosted . " Tags: " . $post->tags . "</span"; echo "

"; } ?>
[/php]

You are passing null value when calling function ‘GetBlogPosts’.

Sponsor our Newsletter | Privacy Policy | Terms of Service