在准备好的语句中使用通配符 - MySQLi [英] Using wildcards in prepared statement - MySQLi

查看:27
本文介绍了在准备好的语句中使用通配符 - MySQLi的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to run the following query, and I'm having trouble with the 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;
    }

我收到一条错误消息:无法通过引用传递参数 2.我需要使用通配符的原因是该列的数据包含序列化数组.我想,如果有更简单的方法来处理这个问题,我能做什么?

I'm getting an error that states: Cannot pass parameter 2 by reference. The reason I need to use the wildcard is because the column's data contains serialized arrays. I guess, if there's an easier way to handle this, what could I do?

提前致谢!

推荐答案

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

You have to pass parameters to bind_param() by reference, which means you have to pass a single variable (not a concatenated string). There's no reason you can't construct such a variable specifically to pass in, though:

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

这篇关于在准备好的语句中使用通配符 - MySQLi的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆