facarroll Posted June 2, 2011 Share Posted June 2, 2011 I have the following code which seems to be in error in the conditional statement. The queries to the database produce the expected results, and the echo statements work OK. I want to make a selection based on whether data exists in the database for passState when passState=1. Can anyone tell me what is wrong with the conditional statement? <?php $query1 = mysql_query("SELECT DISTINCT quizTitle, userId, passState, userScore, totalScore, DATE_FORMAT(userDate,'%b %e, %Y') AS userDate FROM quiz WHERE managerId = '$managerId' AND userId = '$userId' AND passState = 1 ORDER BY quizTitle ASC, passState DESC, userDate DESC "); $query2 = mysql_query("SELECT DISTINCT quizTitle, userId, passState, userScore, totalScore, DATE_FORMAT(userDate,'%b %e, %Y') AS userDate FROM quiz WHERE managerId = '$managerId' AND userId = '$userId' AND passState = 0 ORDER BY quizTitle ASC, passState DESC, userDate DESC "); ?> <table width="778" style="font-size:16px"> <tr> <?php if ($query1) { while ($row1 = mysql_fetch_array($query1)) { echo'<td width="13"></td> <td width="137" style="vertical-align:middle; text-align:left;">' ?> <?php echo "{$row1['quizTitle']} <br />\n"; ?> <?php echo '</td> <td width="13"></td> <td width="132" style="vertical-align:middle; text-align:left;;">' ?> <?php echo "{$row1['userDate']} <br />\n"; ?> <?php echo '</td> <td width="22"></td> <td width="112" style="text-align:center; text-align:center;">' ?> <?php if ("{$row1['passState']}" == 1) {echo "<img src=' ../wood/wood_tool_images/tick2.png' /><br />\n";}?> <?php echo '</td> <td width="30"></td> <td width="103" style="text-align:center; text-align:center;">' ?> <?php if ("{$row1['passState']}" == 0) {echo "<img src=' ../wood/wood_tool_images/cross2.png' /><br />\n";} ?> <?php echo '</td> <td width="19"></td> <td width="126" style="vertical-align:middle; text-align:center;">' ?> <?php echo "{$row1['userScore']}".'/'."{$row1['totalScore']} <br />\n"; ?> <?php echo '</td> <td width="23"></td> </tr>'; } }else{ while ($row2 = mysql_fetch_array($query2)) { echo '<td width="13"></td> <td width="137" style="vertical-align:middle; text-align:left;">' ?> <?php echo "{$row2['quizTitle']} <br />\n"; ?> <?php echo '</td> <td width="13"></td> <td width="132" style="vertical-align:middle; text-align:left;;">' ?> <?php echo "{$row2['userDate']} <br />\n"; ?> <?php echo '</td> <td width="22"></td> <td width="112" style="text-align:center; text-align:center;">' ?> <?php if ("{$row2['passState']}" == 1) {echo "<img src=' ../wood/wood_tool_images/tick2.png' /><br />\n";}?> <?php echo '</td> <td width="30"></td> <td width="103" style="text-align:center; text-align:center;">' ?> <?php if ("{$row2['passState']}" == 0) {echo "<img src=' ../wood/wood_tool_images/cross2.png' /><br />\n";} ?> <?php echo '</td> <td width="19"></td> <td width="126" style="vertical-align:middle; text-align:center;">' ?> <?php echo "{$row2['userScore']}".'/'."{$row2['totalScore']} <br />\n"; ?> <?php echo '</td> <td width="23"></td> </tr>'; } } ?> </table> Quote Link to comment https://forums.phpfreaks.com/topic/238162-having-trouble-with-a-conditional-statement/ Share on other sites More sharing options...
JustLikeIcarus Posted June 2, 2011 Share Posted June 2, 2011 If its in quotes I dont think it can be equal to a number i.e "1" == 1 try if ($row1['passState'] == 1) or maybe if ("{$row1['passState']}" == "1") Quote Link to comment https://forums.phpfreaks.com/topic/238162-having-trouble-with-a-conditional-statement/#findComment-1223864 Share on other sites More sharing options...
mgoodman Posted June 2, 2011 Share Posted June 2, 2011 I'm not quite sure that I understand your problem, but I'm gonna take a shot at it anyways. mysql_query will only return false if there is an error executing the query (even if there are no results). So unless your query is incorrect (MySQL errors) then it will always execute the first block. If you need to do it that way then I suggest using mysql_num_rows. That will tell you how many rows are in the result. Quote Link to comment https://forums.phpfreaks.com/topic/238162-having-trouble-with-a-conditional-statement/#findComment-1223866 Share on other sites More sharing options...
mikesta707 Posted June 2, 2011 Share Posted June 2, 2011 im assuming this if statement if ($query1) { is the condition you want where its true if there are rows returned from $query1, and false if there are no rows returned. If so, use mysql_num_rows(). this returns the number of rows returned from the query. for example if (mysql_num_rows($query1) > 0) {//if there is 1 or more rows returned hope this helps Quote Link to comment https://forums.phpfreaks.com/topic/238162-having-trouble-with-a-conditional-statement/#findComment-1223870 Share on other sites More sharing options...
facarroll Posted June 2, 2011 Author Share Posted June 2, 2011 Thanks to mgoodman and mikesta. Problem solved by you guys. Many thanks. Quote Link to comment https://forums.phpfreaks.com/topic/238162-having-trouble-with-a-conditional-statement/#findComment-1223871 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.