在准备好的语句中使用通配符 - 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);
相关文章