php while loop throwing a error over a $var < 10 statement
Okay, so for some reason this is giving me a error as seen here: http://prime.programming-designs.com/test_forum/viewboard.php?board=0 However if I take away the '&& $cur_row < 10' it works fine. Why is the '&& $cur_row < 10' causing me a problem?
$sql_result = mysql_query("SELECT post, name, trip, Thread FROM (SELECT MIN(ID) AS min_id, MAX(ID) AS max_id, MAX(Date) AS max_date FROM test_posts GROUP BY Thread ) t_min_max INNER JOIN test_posts ON test_posts.ID = t_min_max.min_id WHERE Board=".$board." ORDER BY max_date DESC", $db);
$num_rows = mysql_num_rows($sql_result);
$cur_row = 0;
while($row = mysql_fetch_row($sql_result) && $cur_row < 10)
{
$sql_max_post_query = mysql_query("SELECT ID FROM test_posts WHERE Thread=".$row[3]."");
$post_num = mysql_num_rows($sql_max_post_query);
$post_num--;
$cur_row++;
echo''.$cur_row.'<br/>';
echo'<div class="postbox"><h4>'.$row[1].'['.$row[2].']</h4><hr />' .$row[0]. '<开发者_StackOverflow社区br /><hr />[<a href="http://prime.programming-designs.com/test_forum/viewthread.php?thread='.$row[3].'">Reply</a>] '.$post_num.' posts omitted.</div>';
}
Operator precedence -- assignment (=
) has a lower precedence than &&
.
Your line of code is equivalent to this one (which is fairly obviously flawed):
while($row = (mysql_fetch_row($sql_result) && $cur_row < 10))
You can fix this by using the and
operator instead of &&
, or by using parenthesis.
while($row = mysql_fetch_row($sql_result) and $cur_row < 10)
or
while(($row = mysql_fetch_row($sql_result)) && $cur_row < 10)
Relational and boolean operators have higher precedence than assignment operators:
while(($row = mysql_fetch_row($sql_result)) && $cur_row < 10)
Your error is a MySQL error, caused by the information you're sending MySQL to tell it what to retrieve. It's being caused by this line:
$sql_max_post_query = mysql_query("SELECT ID FROM test_posts WHERE Thread=".$row[3]."");
Is there any reason you have two "" marks at the end of that query line? Whatever is coming back from that request is bad. Not sure why this would have anything to do with the row count, but I would start where the error starts.
精彩评论