問題描述
我正在嘗試運行以下查詢,但在使用通配符時遇到問題.
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的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!