我在通过 mysqli 执行准备好的语句时遇到问题。

首先,我遇到了命令不同步错误。我正在存储结果并关闭连接,并且我已经不再收到此错误,因此希望问题已经停止。

但是,我的 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 的行。

如您所见,brand 被设置为“o”;

所以SQL语句应该是

SELECT ARTICLE_NO FROM AUCTIONS WHERE upper(ARTICLE_NAME) LIKE %o%;

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

有帮助吗?

解决方案

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

使用:

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

或者做:

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

就在命令执行之前。

你不能做的是:

WHERE field LIKE '%?%

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

编辑 在这种情况下,您真正​​的问题似乎是混淆准备好的语句(如 mysqli::preparemysqli_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);
}

其他提示

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

$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

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";
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top