【发布时间】:2016-03-07 10:45:41
【问题描述】:
所以当我手动输入一些值但在php 中我遇到了一些困难时,我的查询可以在实际的phpmysql 服务器上运行。
这是我的SQL 表:
userID | forename | surname | email | age |
------------------------------------------------------------
1 | Jack | Wolf | dj@rave.com | 19 |
2 | Mark | Smith | mark@rave.com | 18 |
3 | Ben | Cas | sex@club.com | 21 |
4 | Jos | Jis | jis@jos.com | 19 |
5 | Luke | Kils | kils@kiss.com | 23 |
------------------------------------------------------------
基本上,我想传入一些UserID 值,例如1,3,5,它应该显示:
userID | forename | surname | email | age |
------------------------------------------------------------
1 | Jack | Wolf | dj@rave.com | 19 |
3 | Ben | Cas | sex@club.com | 21 |
5 | Luke | Kils | kils@kiss.com | 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。我该如何解决?
谢谢
【问题讨论】:
-
简而言之,您不能将单个准备好的参数与
in和可变数量的数组成员一起使用。 -
@apokryfos 那么我该怎么做呢?对不起,我是
php的新手