Query failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL

StackOverflow https://stackoverflow.com/questions/17333278

  •  01-06-2022
  •  | 
  •  

Question

I was forced to update to MySQL 5 and PHP 5 as my service provider is no longer supporting older versions. I have a working site with this MySQL search that was working fine. The following is an include placed on the user's listing page. When I take the SQL statement out and run it on the server it returns the correct result. My guess is that it is something to do with formatting. Any ideas why I might be getting this error?

"Query failed: 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 'ORDER BY x.id_have LIMIT 0,30' at line 1"

With this syntax

<table cellpadding="15" border="0">
 <tr>
  <td valign="top">
   So far, the following members have these items which match;
   <ul>
   <?  
    $peoplewhomatch = mysql_query("SELECT x.id_customer, g.title Have, c.user_name as Member, g.title gwants, gg.title Has, y.id_customer x_username 
        FROM Want x 
        JOIN Want y 
            ON (y.id_sellversion,y.id_version) = (x.id_version,x.id_sellversion) 
        inner join Version as v 
            on x.id_version = v.id_version 
        inner join Version as vv 
            on y.id_version = vv.id_version 
        inner join Game as g 
            on g.id_game = vv.id_game 
        inner join Customer as c 
            on y.id_customer = c.id_customer 
        inner join Game as gg 
            on gg.id_game = v.id_game 
        WHERE x.id_have = $hid 
        ORDER BY x.id_have 
        LIMIT 0, 30")or die("Query failed: " . mysql_error()); 
    while($row = mysql_fetch_array($peoplewhomatch))
    { ?>
    <span class='greenitalic'><?=$row['Member']?></span> has <span class='highshadow'><?=$row['Has']?></span> and wants <span class='ishadow'><?=$row['gwants']?></span><BR><?}?>
  </td>
 </tr>
</table>
Was it helpful?

Solution

You probably need to wrap $hid in quotes like so:

 $peoplewhomatch = mysql_query("SELECT x.id_customer, g.title Have, c.user_name as Member, g.title gwants, gg.title Has, y.id_customer x_username 
        FROM Want x 
        JOIN Want y 
            ON (y.id_sellversion,y.id_version) = (x.id_version,x.id_sellversion) 
        inner join Version as v 
            on x.id_version = v.id_version 
        inner join Version as vv 
            on y.id_version = vv.id_version 
        inner join Game as g 
            on g.id_game = vv.id_game 
        inner join Customer as c 
            on y.id_customer = c.id_customer 
        inner join Game as gg 
            on gg.id_game = v.id_game 
        WHERE x.id_have = '$hid'
        ORDER BY x.id_have 
        LIMIT 0, 30")or die("Query failed: " . mysql_error()); 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top