开发者

Mysql Syntax Error... or is it the PHP?

Can someone help me figure out what is wrong with this function?? I am getting a mysql 开发者_如何学JAVAsyntax error...

function category_exists($name) {
    $name = mysql_real_escape_string($name);

    $query = mysql_query("SELECT COUNT(1) FROM 'categories' WHERE 'name' = '{$name}'"); 
    return (mysql_result($query, 0) == '0')? false : true;
}


You should not have quotes around your table and column name (categories, name). If you need to escape a table or column names, you should use backquotes (`). IE:

$query = mysql_query("SELECT COUNT(1) FROM `categories` WHERE `name` = '{$name}'"); 


function category_exists($name) {
  $name = mysql_real_escape_string($name);

  $query = mysql_query("SELECT COUNT(1) FROM `categories` WHERE `name` = '{$name}'"); 
  return (mysql_result($query, 0) == '0')? false : true;
}

You need either backquotes (`) or NO quotes around table names and field names.


Strings are quoted. Object names (tables, columns...) are not.


Try changing the query to

"SELECT COUNT(*) FROM 'categories' WHERE name = '$name'"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜