Search code examples
mysqlsyntaxwhere-clauseclause

MYSQL where clause causes syntax error


I just cannot find what is wrong with this simple statement:

$stat_qry = mysql_query("SELECT * FROM stats WHERE group=$galgroup") or die("STATS ERROR: ".mysql_error()); $stat = mysql_fetch_array($stat_qry);

i just get: "STATS 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 'group=1' at line 1"

i cannot get it to work with the 'where' clause, if i remove 'where' it works but just lists everything in the database


Solution

  • GROUP is a reserved word so it needs to be between back tick `` Also, if $galgroup can be a string and not only a number, you need to add quotes arround it :

    $stat_qry = mysql_query("SELECT * FROM stats WHERE `group`='$galgroup'") or 
    die("STATS ERROR: ".mysql_error()); $stat = mysql_fetch_array($stat_qry);