RON_ron Posted September 10, 2012 Share Posted September 10, 2012 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) { //// } Quote Link to comment https://forums.phpfreaks.com/topic/268205--/ Share on other sites More sharing options...
spiderwell Posted September 10, 2012 Share Posted September 10, 2012 SQL uses the word OR not the double pipe Quote Link to comment https://forums.phpfreaks.com/topic/268205--/#findComment-1376641 Share on other sites More sharing options...
RON_ron Posted September 10, 2012 Author Share Posted September 10, 2012 thanks spiderwell! Quote Link to comment https://forums.phpfreaks.com/topic/268205--/#findComment-1376642 Share on other sites More sharing options...
kicken Posted September 10, 2012 Share Posted September 10, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/268205--/#findComment-1376649 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.