Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    ivy
    ivy is offline
    Regular Coder
    Join Date
    Jun 2002
    Location
    London / Glasgow / Liverpool
    Posts
    177
    Thanks
    1
    Thanked 0 Times in 0 Posts

    How do I use OR with a SELECT WHERE in php

    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!!!!

    PHP Code:

    $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:

    PHP Code:

    $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!)
    Last edited by ivy; 08-07-2007 at 01:14 AM.
    See ya

    Ivy.

    MAC Codes

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,028
    Thanks
    2
    Thanked 315 Times in 307 Posts
    Use parenthesis to force the logic/order you want. I think you are trying to do this -
    Code:
    where 
    table1.column1 = "1"
    AND
    table2.column1 = "1"
    AND
    (table1.column2 = "1"
    OR
    table2.column2 = "1")
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #3
    ivy
    ivy is offline
    Regular Coder
    Join Date
    Jun 2002
    Location
    London / Glasgow / Liverpool
    Posts
    177
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hi CFMaBiSmAd

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

    Back soon!
    See ya

    Ivy.

    MAC Codes

  • #4
    ivy
    ivy is offline
    Regular Coder
    Join Date
    Jun 2002
    Location
    London / Glasgow / Liverpool
    Posts
    177
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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:

    PHP Code:
    while ($rowcatid mysql_fetch_array($check_idMYSQL_ASSOC)) 
    Which I thought was OK (I was also getting a similar msg before!)
    Last edited by ivy; 08-07-2007 at 01:14 AM.
    See ya

    Ivy.

    MAC Codes

  • #5
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Add error checking to your script.

    PHP Code:
    $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."

  • #6
    ivy
    ivy is offline
    Regular Coder
    Join Date
    Jun 2002
    Location
    London / Glasgow / Liverpool
    Posts
    177
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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
    See ya

    Ivy.

    MAC Codes


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •