arakash Posted November 11, 2011 Share Posted November 11, 2011 Hello everyone! I'm new here on phpfreaks - Here's my problem. I get "1 <br/> 2" echoed, but not the query results. the connect() function connects and selects a table in a mysql database. Here's the code: <?php connect(); $query = "SELECT `title`, `body`, `date` FROM `tutorials` ORDER BY `date` DESC" or die ("Query Error"); $counter = 0; if ($query_run = mysql_query($query)) { while ($query_row = mysql_fetch_assoc($query_run)) && ($counter <= 2) { $title = $query_row['title']; $body = $query_row['body']; $date = $query_row['date']; $counter ++; echo $counter; echo "<br/>"; echo $title; echo $body; echo $date; } } ?> If anyone has any idea, please help! - I'm been battling this for a few hours now Link to comment https://forums.phpfreaks.com/topic/250952-mysql-multiple-conditions-in-while-statement/ Share on other sites More sharing options...
KevinM1 Posted November 11, 2011 Share Posted November 11, 2011 Your while has a syntax error. Should be: while($query_row = mysql_fetch_assoc($query_run) && $counter <= 2) Also, as an aside, you don't need to assign your columns to a variable only to echo them. There's nothing wrong with simply writing: echo $query_row['title']; Link to comment https://forums.phpfreaks.com/topic/250952-mysql-multiple-conditions-in-while-statement/#findComment-1287454 Share on other sites More sharing options...
arakash Posted November 11, 2011 Author Share Posted November 11, 2011 Also, as an aside, you don't need to assign your columns to a variable only to echo them. There's nothing wrong with simply writing: echo $query_row['title']; Hello! Thanks for your quick reply. Hmm.. I think I put them in variables for a reason.. wait, I need to check. I don't remember. haha Link to comment https://forums.phpfreaks.com/topic/250952-mysql-multiple-conditions-in-while-statement/#findComment-1287460 Share on other sites More sharing options...
arakash Posted November 11, 2011 Author Share Posted November 11, 2011 It still doesn't work though. I get 1 "<br/> 2 </br>" Link to comment https://forums.phpfreaks.com/topic/250952-mysql-multiple-conditions-in-while-statement/#findComment-1287461 Share on other sites More sharing options...
kicken Posted November 12, 2011 Share Posted November 12, 2011 while($query_row = mysql_fetch_assoc($query_run) && $counter <= 2) That does not do what your expecting. $query_row is going to be assigned the results of the && operation, not the return value of mysql_fetch_assoc(). So $query_row is going to be either bool(true) or bool(false), not an array with the row content. To fix it, put parenthesis around the assignment: while(($query_row = mysql_fetch_assoc($query_run)) && $counter <= 2) Link to comment https://forums.phpfreaks.com/topic/250952-mysql-multiple-conditions-in-while-statement/#findComment-1287502 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.