Jump to content



Recommended Posts

how do I include a OR to this statement? I tried the below... but no success. Could someone help?

(my idea is to check the name and marks match and at the same time to check if the grade is 1 or 2)


$result=sprintf("SELECT * FROM  db WHERE name = '%s' AND marks ='%s' AND grade = '1' || grade = '2'", mysql_real_escape_string($person), mysql_real_escape_string($mark));

$results = mysql_query($result);

if(mysql_num_rows ($results) == 0) {



Link to comment
Share on other sites

On an additional note, when your mixing AND and OR conditions you should use parenthesis around them to make it clear how they relate to each other.  If I am understanding the precedence in mysql right, what you have would be evaluated as this:

WHERE ((name = '%s' AND marks ='%s') AND grade = '1') OR grade = '2'


Which means you'll get records where grade=2 or (name=%s and marks=%s and grade=1).  In other words, you'll get records where grade=2 regardless of what the name and marks columns are set to.


What you intend (I am guessing) is for the query to be processed as:

WHERE name = '%s' AND marks ='%s' AND (grade = '1' OR grade = '2')


Which will return records where name=%s and marks=%s which have a grade column equal to either 1 or 2.



p.s. side note mysql does allow || (and &&) however OR (and AND) are more typical and standard.


Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • 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.