2013-05-13 55 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