Several ANDs in a row cause error with SELECT, WHERE

Discussion in 'MySQL' started by theblackgold, Dec 4, 2009.

  1. #1
    My error is this:

    "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'inner='y' AND start<=CURDATE()' at line 1"


    The intent of this section of the code is to fill $lft and $rgt with somewhat randomized content from the "item" column that meets several requirements. This code worked perfectly until I added:

    AND inner='y'

    To two of the SELECT statements. When I delete both occurances of this AND, the code will go back to properly selecting content as desired with the exception of taking the "inner" column into account.

    I've done a lot of searching for an answer but I have not found anything. I get the feeling it is something simple that eludes me...



    $bri=rand(0, 1);
    if ($bri==0) {

    $query="SELECT * FROM $table WHERE type='lft' AND subject='chr0001' AND start<=CURDATE()";
    $result=mysql_query($query) or die(mysql_error());

    $num=mysql_numrows($result) - 1;
    $rand=rand(0,$num);

    $lft=mysql_result($result,$rand,"item");
    $lftsub=mysql_result($result,$rand,"subject");
    }

    else {

    $query="SELECT * FROM $table WHERE type='lft' AND subject!='chr0001' AND inner='y' AND start<=CURDATE()";
    $result=mysql_query($query) or die(mysql_error());

    $num=mysql_numrows($result) - 1;
    $rand=rand(0,$num);

    $lft=mysql_result($result,$rand,"item");
    $lftsub=mysql_result($result,$rand,"subject");
    }


    if ($bri==1) {

    $query="SELECT * FROM $table WHERE type='rgt' AND subject='chr0001' AND start<=CURDATE()";
    $result=mysql_query($query) or die(mysql_error());

    $num=mysql_numrows($result) - 1;
    $rand=rand(0,$num);

    $rgt=mysql_result($result,$rand,"item");
    $rgtsub=mysql_result($result,$rand,"subject");
    }

    else {

    $query="SELECT * FROM $table WHERE type='rgt' AND subject!='chr0001' AND inner='y' AND start<=CURDATE()";
    $result=mysql_query($query) or die(mysql_error());

    $num=mysql_numrows($result) - 1;
    $rand=rand(0,$num);

    $rgt=mysql_result($result,$rand,"item");
    $rgtsub=mysql_result($result,$rand,"subject");
    }
     
    theblackgold, Dec 4, 2009 IP
  2. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,334
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #2
    INNER is a reserved word in MySQL and can't be used for a column name.
     
    digitalpoint, Dec 4, 2009 IP
  3. theblackgold

    theblackgold Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for the quick tip, I renamed it and now it works. I knew it was something simple! :p
     
    theblackgold, Dec 4, 2009 IP