尝试使用 json 从 ios 设备通过 php 脚本向服务器发送少量文件名。
如果我使用,我可以发送文件名并接收它们:
$handle = fopen('php://input','r');
$jsonInput = fgets($handle);
$decoded = json_decode($jsonInput,true);
sendResponse(200, json_encode($decoded));
我从 ios 发送了一个 json 字符串 like= ["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($已解码))
或 sendResponse(200, json_encode($decoded));
为什么查询后得到的是null
,毕竟我没有更改$decode
变量?
为什么查询失败?
编辑:::::
删除删除 vardumbs 以获得正确的 json 结果并将连接代码更改为此解决了问题知道我可以获得正确的查询结果。
$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。另外,你写配置的方式似乎有点奇怪,你确定配置的内容是正确获取的吗?
关于php - 使用 PHP 数组时,Select Query 返回 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16526818/