为什么我收到 "You have an error in your SQL syntax" 错误?
Why I'm getting "You have an error in your SQL syntax" error?
我在准备 mysqli 语句时遇到以下错误。
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 ' phone = ?, status = ?, 'date' between ? AND ? ORDER BY id DESC
LIMIT ?' at line 1
mysqli 准备语句:
$stmt = $mysqli->prepare("SELECT id,role,fullname,phone,email,balance,status,parent, date_format(date,'%d-%m-%Y %h:%i:%s %p') AS date FROM users WHERE id = ?, phone = ?, status = ?, 'date' between ? AND ? ORDER BY id DESC LIMIT ?");
if ( false===$stmt )
{
die('prepare() failed: ' . htmlspecialchars($mysqli->error));
}
$rc = $stmt->bind_param('iiissi',$duid,$dmobile,$dstatus,$sdatetime,$edatetime,$dlimit);
if ( false===$rc )
{
die('bind_param() failed: ' . htmlspecialchars($stmt->error));
}
$re = $stmt->execute();
if ( false===$re )
{
die('execute() failed: ' . htmlspecialchars($stmt->error));
}
$stmt->store_result();
$stmt->bind_result($userid, $urn, $uname, $uphone, $uemail, $ubalance, $ustatus, $up, $udate);
$stmt->fetch();
WHERE 子句用 AND
(或 OR
)而不是逗号分隔。您还有另一个语法错误,您将列名 date
放在引号中。应该是蜱虫。引号用于字符串。
$stmt = $mysqli->prepare("SELECT id,role,fullname,phone,email,balance,status,parent, date_format(date,'%d-%m-%Y %h:%i:%s %p') AS date FROM users
WHERE id = ? AND phone = ? AND status = ?
AND `date` between ? AND ? ORDER BY id DESC LIMIT ?");
这是无效语法:
WHERE id = ?, phone = ?
你想要这个:
WHERE id = ? AND phone = ?
或者可能是这样的:
WHERE id = ? OR phone = ?
或者在您的 WHERE
子句中进一步组合显式布尔逻辑。
(重复 WHERE
子句的其余部分。)
我相信您想使用 AND
或 OR
而不是 ,
基本问题
您尝试使用逗号 (,) 组合多个条件,这在语法上是错误的。
快速解决方案
替换
WHERE id = ?, phone = ?
与
WHERE id = ? AND phone = ?
更多解释
在单个 SELECT 语句中只能有一个 WHERE 子句。在单个 WHERE 子句中使用 AND 和 OR 等逻辑运算符组合多个条件。逗号(,)适用于IN子句,可与WHERE子句结合使用。
我在准备 mysqli 语句时遇到以下错误。
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 ' phone = ?, status = ?, 'date' between ? AND ? ORDER BY id DESC LIMIT ?' at line 1
mysqli 准备语句:
$stmt = $mysqli->prepare("SELECT id,role,fullname,phone,email,balance,status,parent, date_format(date,'%d-%m-%Y %h:%i:%s %p') AS date FROM users WHERE id = ?, phone = ?, status = ?, 'date' between ? AND ? ORDER BY id DESC LIMIT ?");
if ( false===$stmt )
{
die('prepare() failed: ' . htmlspecialchars($mysqli->error));
}
$rc = $stmt->bind_param('iiissi',$duid,$dmobile,$dstatus,$sdatetime,$edatetime,$dlimit);
if ( false===$rc )
{
die('bind_param() failed: ' . htmlspecialchars($stmt->error));
}
$re = $stmt->execute();
if ( false===$re )
{
die('execute() failed: ' . htmlspecialchars($stmt->error));
}
$stmt->store_result();
$stmt->bind_result($userid, $urn, $uname, $uphone, $uemail, $ubalance, $ustatus, $up, $udate);
$stmt->fetch();
WHERE 子句用 AND
(或 OR
)而不是逗号分隔。您还有另一个语法错误,您将列名 date
放在引号中。应该是蜱虫。引号用于字符串。
$stmt = $mysqli->prepare("SELECT id,role,fullname,phone,email,balance,status,parent, date_format(date,'%d-%m-%Y %h:%i:%s %p') AS date FROM users
WHERE id = ? AND phone = ? AND status = ?
AND `date` between ? AND ? ORDER BY id DESC LIMIT ?");
这是无效语法:
WHERE id = ?, phone = ?
你想要这个:
WHERE id = ? AND phone = ?
或者可能是这样的:
WHERE id = ? OR phone = ?
或者在您的 WHERE
子句中进一步组合显式布尔逻辑。
(重复 WHERE
子句的其余部分。)
我相信您想使用 AND
或 OR
而不是 ,
基本问题
您尝试使用逗号 (,) 组合多个条件,这在语法上是错误的。
快速解决方案
替换
WHERE id = ?, phone = ?
与
WHERE id = ? AND phone = ?
更多解释
在单个 SELECT 语句中只能有一个 WHERE 子句。在单个 WHERE 子句中使用 AND 和 OR 等逻辑运算符组合多个条件。逗号(,)适用于IN子句,可与WHERE子句结合使用。