Page 1 of 1

MySQL Query/Loop only returns one row.

Posted: Mon Sep 06, 2004 2:14 am
by ripcurlksm
feyd | Please use

Code: Select all

tags when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


I am only getting one row returned from this query....
See it here [url=http://onestopauctionshop.com/preview/test.php?category=Motorcycle%20Parts]http://onestopauctionshop.com/preview/test.php?category=Motorcycle%20Parts[/url]

Here is the code:

Code: Select all

<?php

$query = "SELECT * FROM bookmark WHERE category = '$P_category' AND status='selling' ";
$result = mysql_query($query);

   $bm_URL=mysql_result($result, $rank, "bm_URL");
   $bm_IMG=mysql_result($result, $rank, "bm_IMG");
   $description=mysql_result($result, $rank,"description");



if(mysql_num_rows($result)) {
  $rank = 1;
  while($row = mysql_fetch_array($result, MYSQL_ASSOC))
  {
    print("</tr><tr>");
    if($color == "#666666") {
     $color = "#999999";
    } else {
      $color = "#666666";
    }

   print("<td width="20" bgcolor="$color"><small>");
   print("<font face="Verdana">$rank</font></small></td>");

   print("<td width="20" bgcolor="$color"><small>");
   print("<font face="Verdana">$description</font></small></td>");
   
   print("<td width="20" bgcolor="$color"><center><small>");
   print("<a href="$bm_IMG" target="new"><img src="$bm_IMG" height="50" border="0"></center></td>");
   
   print("<td width="20" bgcolor="$color"><center><small>");
   print("<a href="$bm_URL">Bid on this item!</a></center></td>");
      
  $rank++;
  }
}

?>

feyd | Please use

Code: Select all

tags when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Mon Sep 06, 2004 4:28 am
by m3mn0n
That link returns two results.


And also, post some samples of db information.

BTW [php_man]mysql_num_rows[/php_man]() returns an int, not a boolean var...so line 11 needs to be changed.

Posted: Mon Sep 06, 2004 9:09 am
by Jean-Yves
Sami wrote: BTW [php_man]mysql_num_rows[/php_man]() returns an int, not a boolean var...so line 11 needs to be changed.
Isn't any non-zero value treated as TRUE? So as long as 1 or more records are returned the code within the if() should be executed. I always assumed this.

I too am getting 2 rows returned.

Without a resume of the data, it's impossible to check the validity of the SQL. How many rows do you get when you execute the command in your SQL editor directly?

Posted: Mon Sep 06, 2004 2:15 pm
by m3mn0n
Indeed, but it's bad coding practice IMO.

Posted: Mon Sep 06, 2004 2:50 pm
by Weirdan
Sami wrote:Indeed, but it's bad coding practice IMO.
it's not as long as code is clear and anyone can understand it, IMO. All these tricks (automatic typecasting etc.) are sometimes abused though. There was good book on C++ idioms by Jeff Alger discussing how such a tricks could be used to make your code clear and quick.