MySQL returns 4 results, PHP only 3?

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
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

MySQL returns 4 results, PHP only 3?

Post by JAB Creations »

MySQL returns four results while PHP only returns three and none of the $record['thread_url'] rows are empty, what is the error in the logic of my code? :|

Code: Select all

//$count = mysql_num_rows($mysql_thread_name);
//$count = 4 
 $i=0;
 while($i<=$count)
 {
  $record = mysql_fetch_assoc($mysql_thread_name);
  if (!empty($record['thread_url'])) {echo '<a href="index.php?tag='.strtolower(str_replace(" ", "_", $record['thread_url'])).'">'.htmlspecialchars($record['thread_title']).'</a>';}
  $i++;
  if ($i < $count) {echo ', ';}
 }
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Re: MySQL returns 4 results, PHP only 3?

Post by infolock »

try using your statement like this instead then...

Code: Select all

 
 
echo "<pre>";
print_r($record);
echo "</pre>";
echo "<br />Count is $count<br />";
 
$string = "";
 
for($i = 0; $i <= $count; $i++) {
  $record = mysql_fetch_assoc($mysql_thread_name);
  if (!empty($record['thread_url'])) {
    $string .= '<a href="index.php?tag='.strtolower(str_replace(" ", "_", $record['thread_url'])).'">'.htmlspecialchars($record['thread_title']).'</a>,';
  }
}
 
echo substr($string, 0, -1);
 
and show us the output
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: MySQL returns 4 results, PHP only 3?

Post by JAB Creations »

PHP outputs...
Array([thread_title] => Example 4 [thread_url] => example_4)

Count is 4
Array ([thread_title] => Hello World! [thread_url] => hello_world)

Count is 4
Array ([thread_title] => Quick Sample [thread_url] => quick_sample)
MySQL outputs...

Code: Select all

SELECT thread_title, thread_url FROM xhref_tags LEFT JOIN threads ON xhref_thread_id = thread_id WHERE xhref_tag_id = '3' ORDER BY thread_title ASC
thread_title thread_url
Example 3 example_3
Example 4 example_4
Hello World! hello_world
Quick Sample quick_sample
So somehow PHP is cutting off the first or last entry?
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: MySQL returns 4 results, PHP only 3?

Post by JAB Creations »

My mistake was that I forgot to remove the mysql_fetch_array function that was before the while loop, doh! :| On the bright side if PHP ever short changes me I'll have one more idea of what to try to look for. :mrgreen: Thanks for your reply.
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Re: MySQL returns 4 results, PHP only 3?

Post by infolock »

haha, no problem. i was just about to ask if you were calling mysql_fetch_assoc above that loop, but noticed your last reply at the last minute ;)

happens all the time. glad ya figured it out.
Post Reply