Jump to content


This topic is now archived and is closed to further replies.


Newbie SQL issue!

Recommended Posts

Hello, does anyone have any idea as to why this piece of code returns no record from the database (the record is definately in there):

$query1 = "SELECT * FROM listings WHERE listing_id =1";
$result1 = mysql_query($query1);
$num1 = mysql_num_rows($result1);
if ($num1 > 1) { // results found

echo "$num1 results found.</p>\n";
// Fetch and print all the records.
while ($row = mysql_fetch_array($result1, MYSQL_ASSOC)) {
echo $row['listing_code'];

I have made the code work with a similar SQL query:
"SELECT * FROM listings WHERE site = 1 "

the only differences between the column site and listing_id are that listing_id is int and site smallint plus listing_id is auto increment.
I have been using navicat as a graphical interface to mysql and both the sql statement runs fine in there and returns records.
Any help would be appreciated. Many Thanks

Share this post

Link to post
Share on other sites
change this line

if ($num1 > 1) { // results found

if ($num1 >= 1) { // results found

because listing_id is an auto increment, you always get 1 record with your $query1.

your old code:
if ($num1 > 1) { // results found

only print if there're 2 or more records.


Share this post

Link to post
Share on other sites
Oh GOD! Thank you so much, dumb mistake but I wasted a whole afternoon!!!

Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.