php mysql WHERE子句错误

标签 php mysql where-clause

谁能看出为什么这不起作用?

$category = 1;
$CategoryResult = mysql_query("SELECT * FROM Category WHERE Index = '$category'");
if (!$CategoryResult) 
    die("Could not retrieve results from category $category: " . mysql_error());

Mysql报错如下:

Could not retrieve results from category 1: 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 'Index = '1'' at line 1

Index是表Category的自增主键。

最佳答案

也许索引是一个保留字。试试这个

$CategoryResult = mysql_query("SELECT * FROM Category c WHERE c.`Index` = '$category'");

并检查索引是字符串还是整数

关于php mysql WHERE子句错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5081520/

相关文章:

php - 我应该如何在 mysql_query 函数中编写 PHP $_POST 变量?

mysql - Joomla首次安装错误表不存在

mysql - 多列求和并更新总计字段[MySQL]

sql - 根据别名列名进行过滤

php - MySQL 上的并发计数器

php - 加载类映射后 Composer 自动加载文件

c# - 使用 API 连接到远程 mysql 数据库

php - 插入重复键时,laravel Eloquent 地忽略错误

mysql - SQL SELECT 查询列中的多个值和范围

mysql - 为什么我在 View SELECT 查询中收到带有 WHERE 子句的 "Unknown column ' xyz'"?