简体   繁体   English

Mysql 语法错误……还是 PHP?

[英]Mysql Syntax Error… or is it the PHP?

Can someone help me figure out what is wrong with this function??有人可以帮我弄清楚这个 function 有什么问题吗? I am getting a mysql syntax error...我收到 mysql 语法错误...

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 ).您的表和列名( categoriesname )不应有引号。 If you need to escape a table or column names, you should use backquotes (`).如果需要转义表或列名,则应使用反引号 (`)。 IE: 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.您需要在表名和字段名周围加上反引号 (`) 或 NO 引号。

Strings are quoted.字符串被引用。 Object names (tables, columns...) are not. Object 名称(表、列...)不是。

Try changing the query to尝试将查询更改为

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM