Waaaaat
Waaaaat

Reputation: 644

Select Query Does not Work right

I have a select query which I want to check that if the number of the sensorValue in my database table is not between max and min and the status is equal to 0 then echo a message.

I did something that doesnot work exactly as I want. The code is the below

//ALERT SYSTEM
     $query2 = mysql_query("SELECT username, typeValue, datetime FROM sensors WHERE sensorValue < min OR sensorValue > max AND status='0'");
     while($row = mysql_fetch_array($query2))
     {
        if($username==$row['username'])
        {
            $alert_message= " <b><font color=red><p align='center'>You Have Health Problem with Your ".$row['typeValue']." at ".$row['datetime']."</font></b>";
            $link_address = "viewsensordata.php?view=".$row['username'];
            ?>
            <a href="<?php echo $link_address;?>"> <?php echo $alert_message; ?> </a>
            <?php

        }
     }

What is wrong with the code and it stills echo the message if the status is 1

Upvotes: 0

Views: 61

Answers (2)

Raul Luna
Raul Luna

Reputation: 2046

I'm afraid that you have to put a parenthesis:

SELECT username, typeValue, 
datetime FROM sensors 
WHERE (sensorValue < min OR sensorValue > max) 
AND status='0'

The "problem" here is that the "and" and "or" have precedence and if I am not wrong, that you have been doing is "sensorValue < min or (sensorvalue > and status ='0')"

Upvotes: 6

developerwjk
developerwjk

Reputation: 8659

You need parenthesis:

SELECT username, typeValue, datetime FROM sensors WHERE 
(sensorValue < min OR sensorValue > max)
 AND status='0'

Upvotes: 1

Related Questions