SQL就像语句问题一样

时间:2009-03-06 11:29:01

标签: php sql mysql mysqli

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

当我手动输入时,它可以正常工作。

3 个答案:

答案 0 :(得分:4)

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

使用:

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

或做:

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

在执行命令之前。

你不能做的是:

WHERE field LIKE '%?%

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

编辑在这种情况下,您的真正问题似乎是将准备好的语句(由mysqli::preparemysqli_stmt::execute()支持)与简单的旧查询混合在一起(与{ {3}})。您还应该直接询问数据库服务器的行数,而不是拉取数据并使用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);
}

答案 1 :(得分: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";

答案 2 :(得分:0)

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

$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