【发布时间】:2016-03-06 22:22:08
【问题描述】:
所以当我手动输入一些值但在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'];
$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 = 1,2,3 的事情。 userID 最多只能输入 1 个参数,但我想输入多少就输入多少。
我该如何解决?
【问题讨论】:
-
由于您没有为此发布 HTML 表单,我们不知道
$_POST['userID']是否被视为一个数组,从您发布的内容来看,它不是。那么,如果您正在检查它们,您会得到什么结果和错误?我怀疑你是。 POST 多维数组需要内爆等。 -
嗯,我在做
android开发,我使用postman来测试代码。是的,我不知道如何内爆它或它是什么 -
print_r($_POST);当您发送多个时。 -
使用
$result->bind_param('i', $userID);建议您传递一个整数值,但如果您传递一组逗号分隔的 ID,这将是一个字符串 -
@RamRaider 正是这样,但我不知道该怎么做。请你修改我的代码