View Full Version : Little problem with an AND OR logic !

02-25-2009, 07:25 PM

I am trying to ask the mysql to give my records that
have this:
prod_id="jkw" AND client.no=prod.no AND live="y" OR col="blue"

Now that last OR means if the Live isn't "y" the color can be blue BUT the first two conditions must be correct

Any idea how I need to write this in php ?

So far I have:

$sql = "SELECT * FROM products,clients
WHERE products.prod_id ="jkw"
AND products.no = clients.no
AND live = 'y'
OR col='blue'

I guess that I need some brackets somewhere ?

02-25-2009, 07:32 PM
$sql = "SELECT * FROM products,clients
WHERE products.prod_id ="jkw"
AND products.no = clients.no
AND (live = 'y'
OR col='blue')

02-25-2009, 07:59 PM
Don't forget to fix the prod_id brackets too, otherwise you'll get a nice syntax error.

02-25-2009, 09:14 PM
Thanks for advice.

I have threisd to apply it to my problem (which was a bit more
complicated that I showed in the code), but I am getting at error.

Here is what I wrote:

$sql = "SELECT cat_cd, prod_head, prod_sum, prod_body, prod_pict, prod_link, clients.sc_name
FROM products,clients
WHERE products.prod_id = $N_prod_id
AND products.user_id = clients.user_id
AND ( (products.confirm = 'y' AND products.live = 'y')
OR clients.user_id = $_SESSION['expert'] )

This is the error:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/gffff/public_html/im/dgg6t.php on line 16

Any ideas ?

02-25-2009, 09:39 PM
That error means you're trying to embed a variable in a string that is too complex for a simple embedding; you need to enclose the variable in squiggly brackets {}.

In your case the $_SESSION['expert'] needs such treatment.

See http://us2.php.net/manual/en/language.types.string.php for lots of useful information on all of PHP's string powers. (find the Variable Parsing section for this particular issue)

02-25-2009, 11:47 PM
Fumigator can you please go easy on the technical jargon. some people around here are just new to coding and may be intimidated by such jargon as below

squiggly brackets