因此,我的查詢在實際的phpmysql
服務器上工作時,我手動鍵入一些值,但在php
我有一些困難。如何在WHERE行中放置多個值=(?)SQL php語句
這是我SQL
表:
userID | forename | surname | email | age |
------------------------------------------------------------
1 | Jack | Wolf | [email protected] | 19 |
2 | Mark | Smith | [email protected] | 18 |
3 | Ben | Cas | [email protected] | 21 |
4 | Jos | Jis | [email protected] | 19 |
5 | Luke | Kils | [email protected] | 23 |
------------------------------------------------------------
基本上,我想在一些UserID
值傳遞這樣1,3,5
,它應該顯示:
userID | forename | surname | email | age |
------------------------------------------------------------
1 | Jack | Wolf | [email protected] | 19 |
3 | Ben | Cas | [email protected] | 21 |
5 | Luke | Kils | [email protected] | 23 |
------------------------------------------------------------
的用戶ID值可以根據改變什麼用戶選擇它可以是2
甚至1,2,3,4
或甚至1,2,3,4,5
這是我的php
代碼:
<?php
require "init.php";
if(!empty($_POST['userID'])){
$userID = $_POST['userID'];
echo $_POST['userID'];
$stmt = "SELECT userID, forename, surname, email, age
FROM users
WHERE userID IN (?)";
$result = $conn-> prepare($stmt);
$result->bind_param('i', $userID);
$result->execute();
$outcome=$result->get_result();
$response = array();
if(($outcome->num_rows)>0){
while($row = $outcome->fetch_assoc()){
$response[] = array
(
"userID" => $row["userID"],
"forename" => $row["forename"],
"surname" => $row["surname"],
"email" => $row["email"],
"age" => $row["age"]
);
}
echo json_encode($response);
}
else{
echo json_encode("None found");
}
}
?>
當我回聲$userID = $_POST['userID'];
我得到1,2,3
或1
,但後來這些都沒有被正確地傳遞給SELECT STATEMENT
。我如何解決它?
感謝
[您如何在mysqli預準備語句中使用IN子句](http://stackoverflow.com/questions/772913/how -do-mysqli-prepared-statements) – apokryfos
簡而言之,你不能用'in'和可變數量的數組成員來使用一個準備好的參數。 – apokryfos
@apokryfos,我該怎麼做?對不起,我是'php'的新手[ –