Forum Moderators: coopster

Message Too Old, No Replies

Mysql query help!

For some reason it is not working...

         

PokeTech

5:42 pm on Aug 31, 2009 (gmt 0)

10+ Year Member



I have this code:

$query = mysql_query("SELECT * FROM table WHERE description LIKE '%$search%' OR name LIKE '%$search%' AND pending != 'true'");

and then it displays the results to the user. I'm having problems with the pending part for some reason it's getting all the rows and not getting rid of the ones the are equal to true. I'm not quite sure what to do so I'm asking.

Thanks in advance!

sned

5:50 pm on Aug 31, 2009 (gmt 0)

10+ Year Member



Hmm, I don't think the "!=" works in MySQL (or any SQL?) Try using "<>" instead (also woudn't hurt to throw in some parentheses ...):

SELECT * FROM table WHERE (description LIKE '%$search%' OR name LIKE '%$search%') AND pending <> 'true'";

Edit: Ooops, guess I was wrong about the not equal, probably more has to do with the ()'s:
[dev.mysql.com...]

Demaestro

5:58 pm on Aug 31, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What is the data type of the pending field?

When you put single quotes around the word true.... 'true' you are passing it a string.

If it is boolean you don't want to look for the string 'true' you want to use either true or false... no quotes or use 0 and 1

When using booleans and the words "true" or "false" you can use the keyword 'is' in your statement.

select ..... and pending is true

or

select ..... and pending is not true

rocknbil

1:03 am on Sep 1, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm having problems with the pending part for some reason it's getting all the rows and not getting rid of the ones the are equal to true.

Your problem is most likely in the or:

$query = mysql_query("SELECT * FROM table WHERE description LIKE '%$search%' OR name LIKE '%$search%' AND pending != 'true'");

The "and" will only apply the pending conditional to the "name like ..." in this context. it will select all records where "description like...." Or is a slippery one this way, try using parentheses:

$query = mysql_query("SELECT * FROM table WHERE (description LIKE '%$search%' OR name LIKE '%$search%') AND pending != true");

This way the "and" applies to both of the "or" conditions. Second, it's probably easier to read if "pending" is tested for false (unless it can maintain other values?)

$query = mysql_query("SELECT * FROM table WHERE (description LIKE '%$search%' OR name LIKE '%$search%') AND pending = false");

I'm not sure of the accuracy of the statement about quotes; in my experience it hasn't made any difference whether it's quoted or not, only that for text and decimal fields it must be quoted, for int, tinyint, and boolean fields it's optional.