tags:

views:

39

answers:

4

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]. '<br /><hr />[<a href="http://prime.programming-designs.com/test_forum/viewthread.php?thread='.$row[3].'"&gt;Reply&lt;/a&gt;] '.$post_num.' posts omitted.</div>';
                }
A: 

i can't belive this isn't working, but i'm not able to test it at the moment. as an alternative, why don't you simply do a LIMIT 10 in you SQL-statement?

oezi
+6  A: 

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)
Cory Petosky
Thank you very much. ^_^
William
Simon
+2  A: 

Relational and boolean operators have higher precedence than assignment operators:

while(($row = mysql_fetch_row($sql_result)) && $cur_row < 10)
Ignacio Vazquez-Abrams
A: 

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.

dclowd9901