gpt4 book ai didi

php - 连接失败 : SQLSTATE[42000]: Syntax error or access violation: 1064

转载 作者:行者123 更新时间:2023-11-29 04:33:07 26 4
gpt4 key购买 nike

<分区>

我已经看到很多与我类似的问题,但我的不同:我的错误不是由在 SQL 中使用 “关键字” 引起的!

当我运行我的代码时,如果我取消 WHERE 子句或我使用硬编码值,例如 "WHERE customerNumber = 356" 它工作正常,但如果我尝试使用变量 $custom_n 的值,它会抛出错误。

<?php 
$custom_n = $_POST["emp"];
$servername = "localhost";
$username = "root";
$password = "";

try {
$conn = new PDO("mysql:host=$servername;dbname=classicmodels", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$stmt = $conn -> prepare("SELECT phone, salesRepEmployeeNumber, creditLimit FROM customers WHERE customerNumber = $custom_n");
$stmt -> execute();
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
echo $v;
}
}
catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}

$conn = null;

?>

如果我对 custom_n 和 gettype 进行回显,我得到:353 字符串,这正是我想要的。

这是完整的错误:

Connection failed: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1

根据 Magnus Eriksson 的建议,如果我执行 var_dump($custom_n); 我会得到以下输出:string(12) ""

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com