Page 1 of 1

MySQL returns 4 results, PHP only 3?

Posted: Wed Nov 05, 2008 4:13 pm
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 ', ';}
 }

Re: MySQL returns 4 results, PHP only 3?

Posted: Wed Nov 05, 2008 4:35 pm
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

Re: MySQL returns 4 results, PHP only 3?

Posted: Wed Nov 05, 2008 4:41 pm
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?

Re: MySQL returns 4 results, PHP only 3?

Posted: Wed Nov 05, 2008 5:04 pm
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.

Re: MySQL returns 4 results, PHP only 3?

Posted: Wed Nov 05, 2008 6:15 pm
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.