Php 如何使用mysqli准备的语句绑定N个参数?
在旧的mysql代码中,我在下面有一个查询,它工作得非常好,如下所示:Php 如何使用mysqli准备的语句绑定N个参数?,php,mysqli,Php,Mysqli,在旧的mysql代码中,我在下面有一个查询,它工作得非常好,如下所示: $questioncontent = (isset($_GET['questioncontent'])) ? $_GET['questioncontent'] : ''; $searchquestion = $questioncontent; $terms = explode(" ", $searchquestion); $questionquery = " SELECT q.QuestionId, q.Question
$questioncontent = (isset($_GET['questioncontent'])) ? $_GET['questioncontent'] : '';
$searchquestion = $questioncontent;
$terms = explode(" ", $searchquestion);
$questionquery = "
SELECT q.QuestionId, q.QuestionContent, o.OptionType, an.Answer, r.ReplyType,
FROM Answer an
INNER JOIN Question q ON q.AnswerId = an.AnswerId
JOIN Reply r ON q.ReplyId = r.ReplyId
JOIN Option_Table o ON q.OptionId = o.OptionId
WHERE ";
foreach ($terms as $each) {
$i++;
if ($i == 1){
$questionquery .= "q.QuestionContent LIKE `%$each%` ";
} else {
$questionquery .= "OR q.QuestionContent LIKE `%$each%` ";
}
}
$questionquery .= "GROUP BY q.QuestionId, q.SessionId ORDER BY "; $i = 0; foreach ($terms as $each) {
$i++;
if ($i != 1)
$questionquery .= "+";
$questionquery .= "IF(q.QuestionContent LIKE `%$each%` ,1,0)";
}
$questionquery .= " DESC ";
但是,由于旧的mysql正在逐渐消失,人们说要使用PDO或mysqli(由于我目前使用的是php版本,所以不能使用PDO),我尝试将代码更改为mysqli,但这给我带来了问题。在下面的代码中,我省略了bind_params命令,我的问题是如何绑定下面查询中的参数?它需要能够绑定多个$each
,因为用户能够键入多个术语,并且每个$each
都被归类为一个术语
以下是同一查询的当前mysqli代码:
$questioncontent = (isset($_GET['questioncontent'])) ? $_GET['questioncontent'] : '';
$searchquestion = $questioncontent;
$terms = explode(" ", $searchquestion);
$questionquery = "
SELECT q.QuestionId, q.QuestionContent, o.OptionType, an.Answer, r.ReplyType,
FROM Answer an
INNER JOIN Question q ON q.AnswerId = an.AnswerId
JOIN Reply r ON q.ReplyId = r.ReplyId
JOIN Option_Table o ON q.OptionId = o.OptionId
WHERE ";
foreach ($terms as $each) {
$i++;
if ($i == 1){
$questionquery .= "q.QuestionContent LIKE ? ";
} else {
$questionquery .= "OR q.QuestionContent LIKE ? ";
}
}
$questionquery .= "GROUP BY q.QuestionId, q.SessionId ORDER BY "; $i = 0; foreach ($terms as $each) {
$i++;
if ($i != 1)
$questionquery .= "+";
$questionquery .= "IF(q.QuestionContent LIKE ? ,1,0)";
}
$questionquery .= " DESC ";
$stmt=$mysqli->prepare($questionquery);
$stmt->execute();
$stmt->bind_result($dbQuestionId,$dbQuestionContent,$dbOptionType,$dbAnswer,$dbReplyType);
$questionnum = $stmt->num_rows();
看看这篇文章,它讨论了withbind_param()
的使用
从上面看,上面写着
注意:
结合使用mysqli_stmt_bind_param()时必须小心
使用调用\用户\函数\数组()。请注意,mysqli_stmt_bind_param()
需要通过引用传递参数,而
call_user_func_array()可以接受变量列表作为参数
可以表示引用或值的
你会想用这样的东西
call_user_func_array(array($stmt, 'bind_param'), $terms);
您需要确保SQL字符串$stmt
中出现的?
字符数正确
[编辑]
这里有一个有效的例子
// user entered search strings
$user_terms = array("a", "b", "c");
// append your wildcard "%" to all elements. you must use "&" reference on &$value
foreach ($user_terms as &$value) {
$value = '%'.$value.'%';
}
$types = "";
for($i = 0; $i<sizeof($user_terms); $i++) {
$types .= "s";
}
$terms = array_merge( array($types), $user_terms);
// the array $terms now contains: { "sss", "%a%", "%b%", "%c%" }
$sql = "SELECT ... ?,?,?" // edit your sql here
$stmt = $mysqli->prepare($sql)
call_user_func_array(array($stmt, 'bind_param'), $terms);
//用户输入的搜索字符串
$user_terms=数组(“a”、“b”、“c”);
//将通配符“%”附加到所有元素。必须对&$value使用“&”引用
foreach($user\u术语作为&$value){
$value='%'。$value'%';
}
$types=“”;
对于($i=0;$iprepare($sql)
调用_user_func_数组(数组($stmt,'bind_param'),$terms);
看看这篇文章,它讨论了withbind_param()
的使用
从上面看,上面写着
注意:
结合使用mysqli_stmt_bind_param()时必须小心
使用call_user_func_array()。注意mysqli_stmt_bind_param()
需要通过引用传递参数,而
call_user_func_array()可以接受变量列表作为参数
可以表示引用或值的
你会想用这样的东西
call_user_func_array(array($stmt, 'bind_param'), $terms);
您需要确保SQL字符串$stmt
中出现的?
字符数正确
[编辑]
这里有一个有效的例子
// user entered search strings
$user_terms = array("a", "b", "c");
// append your wildcard "%" to all elements. you must use "&" reference on &$value
foreach ($user_terms as &$value) {
$value = '%'.$value.'%';
}
$types = "";
for($i = 0; $i<sizeof($user_terms); $i++) {
$types .= "s";
}
$terms = array_merge( array($types), $user_terms);
// the array $terms now contains: { "sss", "%a%", "%b%", "%c%" }
$sql = "SELECT ... ?,?,?" // edit your sql here
$stmt = $mysqli->prepare($sql)
call_user_func_array(array($stmt, 'bind_param'), $terms);
//用户输入的搜索字符串
$user_terms=数组(“a”、“b”、“c”);
//将通配符“%”附加到所有元素。必须对&$value使用“&”引用
foreach($user\u术语作为&$value){
$value='%'。$value'%';
}
$types=“”;
对于($i=0;$iprepare($sql)
调用_user_func_数组(数组($stmt,'bind_param'),$terms);
我会试一试,但我猜代码行应该是这样的:调用用户函数数组(array($stmt,'bind_-param'),$each);
?我还需要将%与$each连接起来,因为它是一个like语句吗?是否可以改为这样调用用户函数数组(array($stmt,'bind_-param')),$each='%.$each'%');
好的,我试过:调用用户函数数组(数组($stmt,'bind_param'),$each='%.$each.'%'))
但是,如果我输入了一个正确的术语,例如“AAA”,它就无法找到旧mysql代码中的结果,它确实找到了带有工作代码的resultUpdated答案。诀窍是确保$types
变量的s
字符数与您的搜索$user\u术语数相同,并且必须与?
的字符数相匹配$sql中的ars
我会试一试,但我猜代码行是这样的:调用用户函数数组(array($stmt,'bind_param'),$each);
?我还需要将%与$each连接起来,因为它是一个like语句吗?是否可以改为这样调用用户函数数组(array($stmt,'bind_param')),$each='%.$each'%');
好的,我试过:调用用户函数数组(数组($stmt,'bind_param'),$each='%.$each.'%'))
但是,如果我输入了一个正确的术语,例如“AAA”,它就无法找到旧mysql代码中的结果,它确实找到了带有工作代码的resultUpdated答案。诀窍是确保$types
变量的s
字符数与您的搜索$user\u术语数相同,并且必须与?
的字符数相匹配$sql