将 CSV 传递到 PHP 以在 JSON 中编码多行

Passing CSV into PHP to Encode multiple rows in JSON

Postman is an IDE for making APIs in Chrome.

我将路线定义为:

PHP 案例语句中的逻辑为:

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;

如上所示,将两个 phone 数字作为 CSV 传递,即。 "number1,number2",JSON 结果只有一个对象。但是运行在db里查询,returns两个,应该是:

我知道我在这里遗漏了一些简单的东西。我如何配置它才能工作?

问题出在您的查询上。你的 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)

(你不需要双引号或点)

此外,您应该使用某种准备好的语句或至少转义用户输入。