Jump to content

Having trouble with a conditional statement.


facarroll

Recommended Posts

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>

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.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.