【问题标题】:Passing CSV into PHP to Encode multiple rows in JSON将 CSV 传递给 PHP 以在 JSON 中编码多行
【发布时间】:2015-12-15 08:44:58
【问题描述】:

Postman is an IDE for making APIs in Chrome.

我将路线定义为:

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 结果只有一个对象。但是在数据库中运行查询,返回两个,因为它应该:

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

【问题讨论】:

  • 我不得不反向搜索你的截图,只是为了弄清楚这是什么叫做“邮递员”的东西。您的整个问题都没有提到您寻求帮助的软件的名称。祝你好运..
  • 使用邮递员只是允许将有组织的请求发送到您的服务器进行测试,没什么特别的
  • 好吧,那么标记 SQL.. 对我来说似乎是一个 sql 问题
  • 你的 php 代码已经成熟,可以进行 SQL 注入了。

标签: php json csv postman


【解决方案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)

(你也不需要双引号或点)

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

【讨论】:

    猜你喜欢
    • 2021-12-19
    • 2016-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-11
    • 2021-05-23
    • 2014-10-15
    • 1970-01-01
    相关资源
    最近更新 更多