MySQL Query/Loop only returns one row.

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
ripcurlksm
Forum Commoner
Posts: 34
Joined: Sun Aug 08, 2004 9:17 pm

MySQL Query/Loop only returns one row.

Post 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]
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post 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.
User avatar
Jean-Yves
Forum Contributor
Posts: 148
Joined: Wed Jul 02, 2003 2:13 pm
Location: West Country, UK

Post 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?
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Indeed, but it's bad coding practice IMO.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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.
Post Reply