Jump to content

Newbie SQL issue!


hmjb

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
Link to comment
Share on other sites

change this line

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

to
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.

regards,

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.