...

View Full Version : How do I use OR with a SELECT WHERE in php



ivy
08-06-2007, 08:28 PM
Hello there

Some much needed help required please - it's driving me crazy!

I have a mysql select query in php and would like to...

select all
from a table left joined to another table
where
table1.column1 = "1"
AND
table2.column1 = "1"
AND

this is where it gets tricky for me!

table1.column2 = "1"
OR
table2.column2 = "1"


So far, this is what I am using, and... it doesn't work!!!!




$check_id = mysql_query (' SELECT * FROM
(
categories, subcategories
LEFT JOIN subcategories
ON categories.cid = subcategories.scat_ref
)
WHERE
categories.clink = "'.$this.'"
OR subcategories.slink = "'.$this.'"
AND categories.cdisplay = "1"
AND subcategories.sdisplay = "1"
');




I have done my research and found that OR statements should follow AND statements and so changed to this:




$check_id = mysql_query (' SELECT * FROM
(
categories, subcategories
LEFT JOIN subcategories
ON categories.cid = subcategories.scat_ref
)
WHERE
categories.cdisplay = "1"
AND subcategories.sdisplay = "1"
AND categories.clink = "'.$this.'"
OR subcategories.slink = "'.$this.'"
');



But it still doesn't work!

Help would be really appreciated!

(edited only to change coding to php after reading sticky thread!)

CFMaBiSmAd
08-06-2007, 08:37 PM
Use parenthesis to force the logic/order you want. I think you are trying to do this -

where
table1.column1 = "1"
AND
table2.column1 = "1"
AND
(table1.column2 = "1"
OR
table2.column2 = "1")

ivy
08-06-2007, 08:39 PM
Hi CFMaBiSmAd

I'll give it a go right now...

Back soon!

ivy
08-06-2007, 08:45 PM
Hi again...
Well I got this error msg:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in ....

Which is the following line of code:


while ($rowcatid = mysql_fetch_array($check_id, MYSQL_ASSOC))

Which I thought was OK (I was also getting a similar msg before!)

Fumigator
08-06-2007, 09:00 PM
Add error checking to your script.



$query = "SELECT blah blah";
$result = mysql_query($query);
if (!$result) {
die("SQL Error. Query text: $query<br />Error: ".mysql_error());
}


The error you are getting means "You are trying to use a variable (in your case $check_id) as if it were a query variable but it isn't. The reason it isn't a query resource is probably because the query failed."

ivy
08-06-2007, 10:02 PM
Hi Fumigator

Thank you - I found that I had left a comma out!!!

And thank you too to CFMaBiSmAd - it worked a treat - now, however, I have other issues!!! - could be back!

Once again, thank you both for your kind assistance



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum