[英]SQL like statement problems
我在通過 mysqli 執行准備好的語句時遇到問題。
首先,我遇到了 Command 不同步錯誤。 我正在存儲結果並關閉連接,並且我已經停止收到此錯誤,所以希望問題已經停止。
但是,我的 sql 語法錯誤中的錯誤再次出現,該錯誤在命令不同步時工作正常。 這是我當前的代碼:
我已經嘗試了許多不同的方法來糾正這個 snytax 錯誤,從使用 CONCAT,它在失敗時被注釋掉,從在綁定之前將 % 符號分配給變量等等。沒有任何效果。
嘗試使用:
$numRecords->bind_param("s", "%".$brand."%");
通過引用傳遞會導致錯誤。
<?php
$con = mysqli_connect("localhost", "blah", "blah", "blah");
if (!$con) {
echo "Can't connect to MySQL Server. Errorcode: %s\n". mysqli_connect_error();
exit;
}
$con->query("SET NAMES 'utf8'");
$brand = "o";
$brand = "% ".$brand." %";
echo "\n".$brand;
$countQuery = "SELECT ARTICLE_NO FROM AUCTIONS WHERE upper(ARTICLE_NAME) LIKE ?";
//CONCAT('%', ?, '%')";
echo "\ntest";
if ($numRecords = $con->prepare($countQuery)) {
$numRecords->bind_param("s", $brand);
echo "\ntest bind";
$numRecords->execute();
echo "\ntest exec";
$numRecords->store_result();
$data = $con->query($countQuery) or die(print_r($con->error));
$rowcount = $data->num_rows;
$numRecords->free_result();
$numRecords->close();
echo "/ntest before rows";
$rows = getRowsByArticleSearch("test", "Auctions", " ");
$last = ceil($rowcount/$page_rows);
} else {
print_r($con->error);
}
foreach ($rows as $row) {
$pk = $row['ARTICLE_NO'];
echo '<tr>' . "\n";
echo '<td><a href="#" onclick="updateByPk(\'Layer2\', \'' . $pk . '\')">'.$row['USERNAME'].'</a></td>' . "\n";
echo '<td><a href="#" onclick="updateByPk(\'Layer2\', \'' . $pk . '\')">'.$row['shortDate'].'</a></td>' . "\n";
echo '<td><a href="#" onclick="deleterec(\'Layer2\', \'' . $pk . '\')">DELETE RECORD</a></td>' . "\n";
echo '</tr>' . "\n";
}
function getRowsByArticleSearch($searchString, $table, $max) {
$con = mysqli_connect("localhost", "blah", "blah", "blah");
//global $con;
$recordsQuery = "SELECT ARTICLE_NO, USERNAME, ACCESSSTARTS, ARTICLE_NAME, date_format(str_to_date(ACCESSSTARTS, '%d/%m/%Y %k:%i:%s'), '%d %m %Y' ) AS shortDate FROM $table WHERE upper(ARTICLE_NAME) LIKE '%?%' ORDER BY str_to_date(ACCESSSTARTS, '%d/%m/%Y %k:%i:%s')" . $max;
if ($getRecords = $con->prepare($recordsQuery)) {
$getRecords->bind_param("s", $searchString);
$getRecords->execute();
$getRecords->bind_result($ARTICLE_NO, $USERNAME, $ACCESSSTARTS, $ARTICLE_NAME, $shortDate);
while ($getRecords->fetch()) {
$result = $con->query($recordsQuery);
$rows = array();
while($row = $result->fetch_assoc()) {
$rows[] = $row;
}
return $rows;
}
}
}
確切的錯誤是:
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 '?' at line 11
第 11 行是定義 $countQuery 的行。
如您所見,品牌被稱為“o”;
所以 SQL 語句應該是
SELECT ARTICLE_NO FROM AUCTIONS WHERE upper(ARTICLE_NAME) LIKE %o%;
當我手動放入時效果很好。
mysqli_stmt::bind_param
只能綁定特定的變量,不能綁定表達式。 提供的變量通過引用而不是值傳遞給“綁定”,這意味着底層 SQL 在執行命令時獲取該變量的任何值,而不是在綁定時。
利用:
WHERE field LIKE CONCAT('%', ?, '%")
或者做:
$brand = '%' . $brand . '%'
在命令執行之前。
你不能做的是:
WHERE field LIKE '%?%
因為?
綁定變量必須對應於單個字符串或數值,而不是 substring(或字段名稱)。
編輯在這種情況下,您的真正問題似乎是將准備好的語句(由mysqli::prepare
和mysqli_stmt::execute()
支持)與普通的舊查詢(與mysqli::query()
一樣)。 您還應該直接從數據庫服務器詢問行數,而不是提取數據並使用 num_rows:
$countQuery = "SELECT COUNT(ARTICLE_NO) FROM AUCTIONS WHERE upper(ARTICLE_NAME) LIKE ?";
if ($numRecords = $con->prepare($countQuery)) {
$numRecords->bind_param("s", $brand);
$numRecords->execute();
$numRecords->bind_result($num_rows);
$numRecords->fetch();
$numRecords->free_result();
$numRecords->close();
$last = ceil($rowcount/$page_rows);
} else {
print_r($con->error);
}
This post on mysql.com seems to suggest that CONCAT()
should work: http://forums.mysql.com/read.php?98,111039,111060#msg-111060
您是否嘗試過使用命名參數?
您嘗試致電:
$numRecords->bind_param("s", "%".$brand."%");
但是您的 sql 是:
$countQuery = "SELECT ARTICLE_NO FROM AUCTIONS WHERE upper(ARTICLE_NAME) LIKE ?";
不應該嗎? (注意LIKE?s
)
$countQuery = "SELECT ARTICLE_NO FROM AUCTIONS WHERE upper(ARTICLE_NAME) LIKE ?s";
問題不在於准備好的語句,而在於對不應該存在的“查詢”方法的調用 - 因為它用於執行“標准”(未准備好的)語句。
$data = $con->query($countQuery) or die(print_r($con->error));
$rowcount = $data->num_rows;
應該
$rowcount = $numRecords->num_rows;
在 getRowsByArticleSearch function 中,您應該再次刪除查詢並使用傳遞給 output 的 bind_result 的變量:
$getRecords->bind_result($ARTICLE_NO, $USERNAME, $ACCESSSTARTS, $ARTICLE_NAME, $shortDate);
while ($getRecords->fetch()) {
$pk = $ARTICLE_NO;
echo '<tr>' . "\n";
echo '<td><a href="#" onclick="updateByPk(\'Layer2\', \'' . $pk . '\')">'.$USERNAME.'</a></td>' . "\n";
// etc...
}
有關更多信息,請查看 PHP 的 bind_result 手冊: http://php.net/manual/en/mysqli-stmt.bind-result.php
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.