2015-12-14 43 views
0

Postman is an IDE for making APIs in Chrome.傳遞CSV到PHP編碼的多個行中JSON

我已路由定義爲:

enter image description here

隨着在PHP case語句邏輯爲:

case "checkContactsWithServer": 

     if ($userId = authenticateUser($db, $username, $password, $gcmregid)) 
     { 
      $phoneNumCSV = $_REQUEST['phone_num_csv']; 

      $syncContactsServerSQL = "SELECT DISTINCT 
             source_user_sync_id, target_user_sync_id, friend_request_sync_id, status, user_sync_id, phone_number 
            FROM friends a 
            RIGHT JOIN (SELECT distinct 
                user_sync_id, 
                phone_number 
               FROM users 
               WHERE phone_number IN ('".$phoneNumCSV."') 
               AND user_sync_id != '".$userId."' 
               ) b 
             ON a.target_user_sync_id = '".$userId."' 
             AND a.source_user_sync_id = b.user_sync_id          
             OR a.source_user_sync_id = '".$userId."' 
             AND a.target_user_sync_id = b.user_sync_id;"; 

      if($result = $db->query($syncContactsServerSQL)) 
      { 

       $rows = array(); 

       while($row = $result->fetch_assoc()){ 
        $rows[] = array('data' => $row); 
       } 

       // now all the rows have been fetched, it can be encoded 
       echo json_encode($rows);  

      } 
      else 
      { 
       $out = FAILED;   
      }  


     } 
     else 
     { 
      error_log($out, 0); 
      $out = FAILED; 
     } 


    break; 

從上面可以看出,將兩個電話號碼作爲CSV傳遞,即。 「number1,number2」,JSON結果只有一個對象。但運行在數據庫中查詢,返回兩個,因爲它應該:

enter image description here

我知道我失去了一些東西簡單在這裏。我怎樣才能配置這個工作?

+0

我不得不扭轉圖片搜索你的截圖只是爲了弄清楚這是所謂的「郵遞員」。你的整個問題你都沒有提過你要求幫助的軟件名稱。祝你好運.. –

+0

使用郵遞員只是允許發送有組織的請求到您的服務器進行測試,沒有什麼特別的 – Sauron

+0

好吧然後,如何回合標記SQL ..似乎是一個SQL問題對我 –

回答

1

問題是您的查詢。你的IN條款全都嚇壞了。

WHERE phone_number IN ('".$phoneNumCSV."') 

變爲...

WHERE phone_number IN ('1231231234,1231231234') 

正確的語法是

WHERE phone_number IN ('1231231234','1231231234') 

您可以通過更改這一行做到這一點..

$phoneNumCSV = $_REQUEST['phone_num_csv']; 

爲了這個..

$phoneNumCSV = "'".implode("','", explode(",", $_REQUEST['phone_num_csv']))."'"; 

然後刪除查詢中的引號。

WHERE phone_number IN ($phoneNumCSV) 

(你不需要雙引號或點要麼)

此外,您應該使用某種事先準備好的聲明的或至少越獄用戶輸入。