Mysql error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yasmine
    New Member
    • Feb 2008
    • 64

    Mysql error

    Hi friends,

    I don't know whether this thread is related to this forum or php forum.
    Please forgive me, if i had done this mistake.

    Can anyone tell me is it possible to write a query attached with php variable in where clause?

    for example,

    [CODE=text]$nm=implode("," ,$si[name]);
    $result=get_rec ords_sql("selec t name, id from a where name in ($nm)");
    echo $result;[/CODE]

    But this query shows me the following error:

    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 'something' at line 1.

    can anybody tell me what is it and what i have to do to fix this?
    Please help me out.

    Thanx n Regards
    Yas...
  • debasisdas
    Recognized Expert Expert
    • Dec 2006
    • 8119

    #2
    You need to concatinate the variable with sql ,can't include that directly .

    Again the variable in the first line might return multiple values. That might be an array .

    Comment

    • mwasif
      Recognized Expert Contributor
      • Jul 2006
      • 802

      #3
      Originally posted by yasmine
      [CODE=text]$nm=implode("," ,$si[name]);
      $result=get_rec ords_sql("selec t name, id from a where name in ($nm)");
      echo $result;[/CODE]
      If $si[name] is string then you must have to use ',' as a separater for IN () and make sure you have some value in $nm.

      Try the below code
      [CODE=php]$nm=implode("', '",$si["name"]);
      $result=get_rec ords_sql("selec t name, id from a where name in ('$nm')");
      echo $result;[/CODE]
      If you still face any problem then use mysql_error() to get the exact error message and post here.

      Comment

      • yasmine
        New Member
        • Feb 2008
        • 64

        #4
        Originally posted by mwasif
        If $si[name] is string then you must have to use ',' as a separater for IN () and make sure you have some value in $nm.

        Try the below code
        [CODE=php]$nm=implode("', '",$si["name"]);
        $result=get_rec ords_sql("selec t name, id from a where name in ('$nm')");
        echo $result;[/CODE]
        If you still face any problem then use mysql_error() to get the exact error message and post here.

        Thanks a lot mwasif.
        I didn't use single quotes for separation. Now its working well.


        Thank u very much....

        Regards
        Yas....

        Comment

        • mwasif
          Recognized Expert Contributor
          • Jul 2006
          • 802

          #5
          You are welcome.

          Comment

          Working...