mysql - mysql_fetch_array returns nothing while mysql_num_rows returns 4 in php -
i have code in php accesses mysql database in sorted order, prints out info in table. however, whenever calls mysql_fetch_array, function returns nothing, though mysql_num_rows 4. is, if $row=mysql_fetch_array($result);, $row['name'] "" though there name column in table. snippet of code below:
<?php include_once("include.php"); $number=0; if(!isset($_get['scores'])) { $number=10; } else if((int )$_get['scores']>100) { $number=100; } else if((int) $_get['scores']<0) { $number=10; } else { $number=(int) $_get['scores']; } $con=mysql_connect($dbhost, $dbuser, $dbpass); mysql_select_db($dbname, $con); $result=mysql_query("select * ".$dbtable_scores." order score desc,date;", $con); $num=1; echo("<table>"); echo("<tr><td>position</td><td>name</td><td>score</td><td>date</td></tr>"); while($row=mysql_fetch_array($result) && $num<=$number) { echo("<tr>"); echo("<td>".$num."</td>"); echo("<td>".$row['name']."</td><td>".$row['score']."</td><td>".$row['date']."</td>"); echo("</tr>"); $num++; } echo("</table>"); mysql_close($con); ?>
i have checked query in mysql cli, , seems work fine. however, see if go http://mtgames.org/index.php, table has numbers not generated mysql. mysql table has columns name, score, date, among others. appreciated. thanks, -michael.
$row
not being set result of mysql_fetch_array()
, it's being set mysql_fetch_array() && $num <= $number
, equal true
. might work if put parentheses around ($row = mysql_fetch_array($result))
save heartache moving && if statement wraps contents of while.
a @ http://php.net/manual/en/language.operators.precedence.php confirms this. &&
has higher operator precedence assignment, =
. however, and
has lower precedence. never knew that! why, oh why, php?
Comments
Post a Comment