1

我在通过 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%;

当我手动放入时效果很好。

4

3 回答 3

4

mysqli_stmt::bind_param只能绑定特定的变量,不能绑定表达式。提供的变量通过引用而不是值传递给“绑定”,这意味着底层 SQL 在执行命令时获取该变量具有的任何值,而不是在绑定时。

采用:

WHERE field LIKE CONCAT('%', ?, '%")

或者做:

$brand = '%' . $brand . '%'

在命令执行之前。

你不能做的是:

WHERE field LIKE '%?%

因为?绑定变量必须对应于单个字符串或数值,而不是子字符串(或字段名称)。

编辑在这种情况下,您的真正问题似乎是将准备好的语句(由mysqli::prepareand支持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);
}
于 2009-03-06T12:08:15.727 回答
0

mysql.com上的这篇文章似乎表明CONCAT()应该有效: 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";
于 2009-03-06T11:52:48.557 回答
0

问题不在于准备好的语句,而在于对不应该存在的“查询”方法的调用 - 因为它用于执行“标准”(未准备好的)语句。

$data = $con->query($countQuery) or die(print_r($con->error));
$rowcount = $data->num_rows;

应该

$rowcount = $numRecords->num_rows;

在 getRowsByArticleSearch 函数中,您应该再次删除查询并使用传递给 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

于 2009-03-06T13:04:38.620 回答