在舊版本的MySQL的代碼,我有低於該查詢工作完全是在下面:如何使用mysqli預處理語句綁定N個參數?
$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的(不能使用,因爲PDO的版本的PHP我目前得到),我試着改變我的代碼到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();
我會搏一搏,但我猜的代碼行會是這樣的:'call_user_func_array(陣列($語句,'bind_param'),$ each); '?我也需要將$與$ each連接起來,因爲它是一個LIKE語句?莫非這樣代替'call_user_func_array(陣列($語句, 'bind_param'),$每= '%' $各 '%'。); '? – user1394925
好的,我嘗試這樣:'call_user_func_array(陣列($語句, 'bind_param'),$每= '%' $各 '%'。); '但是如果我在正確的詞語輸入諸如「AAA」,它無法找到結果作爲舊版本的MySQL代碼,它沒有找到工作的結果代碼 – user1394925
更新的答案。關鍵是要確保'$ types'變量具有相同數量的's'字符作爲搜索$ user_terms也必須在'$ sql'匹配的'?'字符數 – Brad