Jump to content


Photo

Newbie SQL issue!


  • Please log in to reply
2 replies to this topic

#1 hmjb

hmjb
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 01 June 2006 - 03:57 AM

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

#2 hvle

hvle
  • Members
  • PipPipPip
  • Advanced Member
  • 667 posts
  • Locationmelbourne, Australia

Posted 01 June 2006 - 05:15 AM

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,


Life's too short for arguing.

#3 hmjb

hmjb
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 01 June 2006 - 08:56 PM

Oh GOD! Thank you so much, dumb mistake but I wasted a whole afternoon!!!








0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users