尝试通过php脚本从ios设备使用json发送几个文件名到服务器。
我可以发送文件名并接收它们,如果我使用:
$handle = fopen('php://input','r');
$jsonInput = fgets($handle);
$decoded = json_decode($jsonInput,true);
sendResponse(200, json_encode($decoded));
我从ios发送了一个json字符串,比如= ["sample.pdf","sample-1.pdf"]
sendResponse 200在ios设备中,这证明我的发送和接收方法工作=
(
"sample.pdf",
"sample-1.pdf"
)
然而,如果我将代码更改为下面并尝试运行查询,我总是得到Query failed
$handle = fopen('php://input','r');
$jsonInput = fgets($handle);
$decoded = json_decode($jsonInput,true);
var_dump($decoded);
$names = join("', '",$decoded);
var_dump($names);
$sql = new mysqli($config['localhost'], $config['xxx'], $config['xxxx'], $config['xxxxx']);
if (mysqli_connect_errno()) {
printf("Connect failed: %s'n", mysqli_connect_error());
exit;
}
$query = "SELECT * FROM FILES WHERE name IN ('$names')";
$result = $sql->query($query);
if (!$result) {
var_dump($result);
//printf("Query failed: %s'n", $mysqli->error);
//sendResponse(417, json_encode("Query failed:"));
sendResponse(417, json_encode($decoded));
exit;
}
$rows = array();
while($row = $result->fetch_row()) {
$rows[]=$row;
}
sendResponse(200, json_encode($decoded));
$result->close();
$sql->close();
从上面的代码,我总是得到Query failed
响应,但真正奇怪的是,我也得到sendResponse(417, json_encode($decoded))
或sendResponse(200, json_encode($decoded));
的NULL
响应
为什么我查询后得到null
,毕竟我没有改变$decode
变量?
为什么查询失败?
编辑:::::
删除删除正确json结果的vardumbs并更改连接代码以解决问题,知道我可以获得正确的查询结果。
$sql = new mysqli('localhost', 'user', 'password', 'database');
if (mysqli_connect_errno()) {
printf("Connect failed: %s'n", mysqli_connect_error());
exit;
}
我看到你在混淆mysqli的语法。
$sql = new mysqli($config['localhost'], $config['xxx'], $config['xxxx'], $config['xxxxx']);
if ($mysqli->connect_error) {
你应该使用面向对象的符号来对应使用new。此外,你是如何写配置似乎有点奇怪,你确定配置的内容被正确获得吗?