php - 在准备好的语句中使用通配符

标签 php mysqli prepared-statement wildcard

我正在尝试运行以下查询,但在使用通配符时遇到了问题。

function getStudents() {
    global $db;
    $users = array();
    $query = $db->prepare("SELECT id, adminRights FROM users WHERE classes LIKE ? && adminRights='student'");
    $query->bind_param('s', '%' . $this->className . '%');
    $query->execute();
    $query->bind_result($uid, $adminRights);
    while ($query->fetch()) {
        if (isset($adminRights[$this->className]) && $adminRights[$this->className] == 'student')
            $users[] = $uid;
    }
    $query->close();
    return $users;
}

我收到一条错误消息:

Cannot pass parameter 2 by reference.

我需要使用通配符的原因是列的数据包含序列化数组。我想,如果有更简单的方法来处理这个问题,我该怎么办?

最佳答案

您必须通过引用将参数传递给 bind_param(),这意味着您必须传递一个单个变量(而不是一个串联的字符串)。不过,您没有理由不能专门构造这样一个变量来传入:

$className = '%' . $this->className . '%';
$query->bind_param('s', $className);

关于php - 在准备好的语句中使用通配符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1352002/

相关文章:

C++ SQLite3 准备删除语句不工作

php - 使用 php 在 div 上单击计数器

php - 通过 jQuery ajax 发送的内容被截断

php - 使用 php 将我的数据库导出到 csv

php - mysql插入问题

php - 从模型获取数据时为 foreach 提供的参数无效

php - mysqli - 有多少就太多了(非常慢)

php - PDO、准备好的语句和 SQL 注入(inject)

php - 带有内部选择的复杂 SQL 插入更新

php - 如何通过 paypal IPN 发送和检索自定义表单信息?