连接失败:SQLSTATE [42000]:语法错误或访问冲突:1064
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了连接失败:SQLSTATE [42000]:语法错误或访问冲突:1064相关的知识,希望对你有一定的参考价值。
我已经看到很多类似于我的问题,但是我的不一样:我的错误不是因为在SQL中使用"keyword"
引起的!
当我运行我的代码,如果我取消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字符串这是我想要的。
这是完整的错误:
连接失败:SQLSTATE [42000]:语法错误或访问冲突:1064 SQL语法中有错误;检查与您的MariaDB服务器版本对应的手册,以便在第1行的''附近使用正确的语法
正如Magnus Eriksson所建议的,如果我做一个var_dump($custom_n);
我得到这个输出:string(12) ""
$stmt = $conn -> prepare("SELECT phone, salesRepEmployeeNumber, creditLimit FROM customers WHERE customerNumber = ?");
$stmt->execute(array($custom_n));
你的代码看起来很好,所以我假设你的字符串转义查询。下次尝试使用预备语句。
正如Magnus Eriksson所建议的那样,我的POST方法存在问题......即使它似乎没问题,我的变量custom_n也有一个奇怪的var_dump输出string(12)""
。我记得我以前使用过相同的代码而且工作正常,所以我只是修改代码完全相同:在我有发送“POST变量”的表单的页面中,我使用了这段代码:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$custom_n = array();
try {
$conn = new PDO("mysql:host=$servername;dbname=classicmodels", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn -> prepare("SELECT checkNumber, paymentDate, amount, customerNumber FROM payments ORDER BY paymentDate DESC");
$stmt -> execute();
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
echo $v;
if ($k == 'customerNumber') {
array_push($custom_n, $v);
}
}
$tableCodes = "<table id='buttons'>";
foreach ($custom_n as $c) {
$tableCodes .= "
<tr>
<td>
<form action='customers.php' method='post' target='POPUPW' onsubmit="POPUPW = window.open('about:blank','POPUPW', 'width=800px,height=600px');">
<button type='submit' name='code' value='$c'>Details</button>
</form>
</td>
</tr>
";
}
$tableCodes .= "</table>";
echo $tableCodes;
}
catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
$conn = null;
?>
我以为我可以节省一些时间从另一个查询获取custom_n,我在该页面中填充表而不是进行另一个查询。我不确定这有什么问题,但随着这种变化,我解决了这个问题:
<?php
$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 checkNumber, paymentDate, amount, customerNumber FROM payments ORDER BY paymentDate DESC");
$stmt -> execute();
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v) {
echo $v;
}
$customQuery = $conn -> prepare("SELECT customerNumber FROM payments ORDER BY paymentDate DESC");
$customQuery -> execute();
$custom = $customQuery->fetchAll(PDO::FETCH_COLUMN);
$tableCodes = "<table id='buttons'>";
foreach ($custom as $c) {
$tableCodes .= "
<tr>
<td>
<form action='customers.php' method='post' target='POPUPW' onsubmit="POPUPW = window.open('about:blank','POPUPW', 'width=800px,height=600px');">
<button type='submit' name='emp' value='$c'>Details</button>
</form>
</td>
</tr>
";
}
$tableCodes .= "</table>";
echo $tableCodes;
}
catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
$conn = null;
?>
我要感谢大家,特别是MagnusEriksson,MasivuyeCokile和Pr1nc3,而我对参数化的准备语句一无所知。非常感谢,如果你发现为什么以前的代码不起作用,请随时评论:D。
首选方式是绑定。你可以像这样绑定param。你可以参考here
$stmt = $conn -> prepare("SELECT phone, salesRepEmployeeNumber, creditLimit FROM customers WHERE customerNumber = :customernumber");
$stmt->bindParam(':customernumber', $custom_n, PDO::PARAM_INT);
以上是关于连接失败:SQLSTATE [42000]:语法错误或访问冲突:1064的主要内容,如果未能解决你的问题,请参考以下文章
从服务器检索数据到本地返回 SQLSTATE[42000]:语法错误或访问冲突:1142 SELECT 命令拒绝用户
Illuminate\Database\QueryException:SQLSTATE[42000]:语法错误或访问冲突:1064
Laravel 5.8 SQLSTATE [42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误
带有消息“SQLSTATE [42000]”的未捕获异常“PDOException”:语法错误或访问冲突: