No results from mysql query.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Richard66
Forum Newbie
Posts: 4
Joined: Thu Jul 21, 2011 5:09 am

No results from mysql query.

Post by Richard66 »

Hi all, I have the following code and the relevant database and tables set up on a localhost. Yet the query is returning nothing. No error message or results.

Code: Select all

<?php

$db_host = 'localhost';
$db_user = 'root';
$db_pass = 'root';
$db_database = 'bbg_db_2'; 

$dbc = mysql_connect($db_host,$db_user,$db_pass);
$sdb = mysql_select_db($db_database);

$query = "SELECT category_name, category_desc FROM categories";
$result = mysql_query($sdb, $dbc, $query)
or die (mysql_error($dbc));

while($row = mysql_fetch_array($result)) {
    
$catname = $row["category_name"];
$catdesc = $row["category_desc"];
    
echo "<li>$catname</br><span>$catdesc</span></a></li>";

}

?>
If anyone has any ideas why this is not working I would be very grateful. Many thanks in advance.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: No results from mysql query.

Post by social_experiment »

What results do you receive if you modify the $result variable like the example below

Code: Select all

<?php
$result = mysql_query($sdb, $dbc, $query);
# try
$result = mysql_query($query, $dbc);
?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Richard66
Forum Newbie
Posts: 4
Joined: Thu Jul 21, 2011 5:09 am

Re: No results from mysql query.

Post by Richard66 »

Your spot on. Thankyou so much. I was storing the result from the mysql_select_db in the wrong place. I don't need to store it and also as you have shown the $query should come before the database connection in $result.
Post Reply