2013-05-13 126 views
1

尝试通过使用json的ios设备通过php脚本向服务器发送少量文件名。使用PHP Array时Select Query返回NULL

我可以发送文件名,并接受他们回来,如果我使用:

$handle = fopen('php://input','r'); 
    $jsonInput = fgets($handle); 
    $decoded = json_decode($jsonInput,true); 

    sendResponse(200, json_encode($decoded)); 

我发送一个JSON字符串从IOS像= ["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; 
    } 
+0

按照mysqli_connect_error()给出了一个特定的错误吗? – pal4life 2013-05-13 16:29:09

+0

请问你可以做一个'echo $ query;'并向我们显示打印的值? – 2013-05-13 16:31:41

+0

而不是var_dump(名称),放一个echo $名称,只是为了看看它是否构建得很好 – Hackerman 2013-05-13 16:31:55

回答

1

我看你是混合mysqli的语法。

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

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

+0

凭据是正确的,但改变的方法' $ sql = new mysqli('localhost','xxxx','xxxxx','xxxxx'); \t if(mysqli_connect_errno())'解决了问题 – 2013-05-13 17:16:43