1

尝试使用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响应,但真正奇怪的是我也得到NULL了响应sendResponse(417, json_encode($decoded))orsendResponse(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;
    }
4

1 回答 1

1

我看到你正在混合 mysqli 的语法。

$sql = new mysqli($config['localhost'], $config['xxx'], $config['xxxx'],   $config['xxxxx']);
if ($mysqli->connect_error) {

您应该使用面向对象的表示法来对应使用 new。另外,您如何编写配置似乎有点奇怪,您确定正确获取配置的内容吗?

于 2013-05-13T16:59:45.953 回答