【问题标题】:Echoing an array using json_encode使用 json_encode 回显数组
【发布时间】:2015-07-04 12:59:25
【问题描述】:

我正在使用 javascript 应用程序向服务器端请求搜索功能。它适用于某些查询,但不适用于其他查询。

对于某些查询,GET 请求没有返回任何响应,尽管在工作台中测试查询会返回数千条记录。我尝试打开错误 - 没有产生错误。我尝试增加内存限制 - 这不是罪魁祸首。我尝试输出 PDO 错误/警告 - 没有生成任何内容,PDO 实际上返回了记录,我使用 var_dump 验证了这一点,如下所示。

因此,总而言之,以下代码中的所有内容似乎都可以完美运行,直到负责将数组编码为 json 对象的最后一行 - 它没有任何回应。

感谢您为解决此问题提供的任何帮助。

PHP

error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('memory_limit', '2048M');
$db = new PDO('mysql:host=localhost;dbname=mydb', 'root', '');

$search = $_GET['search'];
$searchBy = ( isset($_GET['searchBy']) && !empty($_GET['searchBy']) ) ? $_GET['searchBy'] : 'name';


$sql = "SELECT * FROM business WHERE name LIKE '%university%' GROUP BY id"; 
$query = $db->prepare($sql);
$query->execute();
$results = $query->fetchAll(); //query returns 5000+ records in under a second in workbench

$headers = array('Business', 'City', 'State', 'Zip', 'Phone');


$json = array("results" => $results, 'headers' => $headers, 'query' => $sql);
var_dump($json); //prints successfully 

echo json_encode($json); // echos nothing!

【问题讨论】:

  • var_dump(json_encode($json)); 的 exact 输出是什么?
  • 然后改变它,例如如果你得到:string(7) "[1,2,3]" -> 写类似:string(7) "[X,X,X]"
  • 将它存储在文件中一段时间​​,这样当它出错时,你就会得到实际生成的内容。
  • @AnchovyLegend 完全空白所以你甚至没有得到这个:string(0) ""(还要确保你显示在源代码中!)?
  • 总是运行一些有用的东西! :) 很高兴它被整理出来了。

标签: php mysql json pdo


【解决方案1】:

编辑:

使用 utf8_encode() 然后 json_encode() 如此处所述 (Special apostrophe breaks JSON)

或

$dbHandle = new PDO("mysql:host=$dbHost;dbname=$dbName;charset=utf8", $dbUser, $dbPass,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));

将解析utf8中的所有数据。

旧

这样做会有什么结果?

echo json_encode($results);
//scratch those
//$json = array("results" => $results, 'headers' => $headers, 'query' => $sql);
//var_dump($json); //prints successfully 

你的内存可能用完了吗?尝试 100 个结果。我从其他项目的经验中知道 json_encode 可以使用共享服务器的内存。

很抱歉,如果我没有您希望我提供的帮助,但我们无法真正测试您的代码,您必须为我们完成。

【讨论】:

    【解决方案2】:

    前几天我尝试使用 json_encode 时遇到了这个确切的错误。它没有返回任何错误......只是一个空白屏幕,最后它击中了我。问题在于字符编码。我尝试使用的列已被文案人员使用,他们从 Microsoft Word 直接剪切并粘贴到所见即所得。

    在你的 json_encode 之后回显 json_last_error() 看看你得到了什么

    .
    .
    .
    echo json_encode($json); // echos nothing!
    echo json_last_error(); // integer if error hopefully 0
    

    它应该返回一个整数,指定以下错误之一。

    0 = JSON_ERROR_NONE
    1 = JSON_ERROR_DEPTH
    2 = JSON_ERROR_STATE_MISMATCH
    3 = JSON_ERROR_CTRL_CHAR
    4 = JSON_ERROR_SYNTAX
    5 = JSON_ERROR_UTF8
    

    在我的情况下,它返回一个 5。然后我必须清理每个“描述”列,然后它才能工作。如果它是相同的错误,我已经包含了您需要运行所有列以清除它们的功能。

    function utf8Clean($string)
    {
        //reject overly long 2 byte sequences, as well as characters above U+10000 and replace with *
        $string = preg_replace('/[\x00-\x08\x10\x0B\x0C\x0E-\x19\x7F]'.
         '|[\x00-\x7F][\x80-\xBF]+'.
         '|([\xC0\xC1]|[\xF0-\xFF])[\x80-\xBF]*'.
         '|[\xC2-\xDF]((?![\x80-\xBF])|[\x80-\xBF]{2,})'.
         '|[\xE0-\xEF](([\x80-\xBF](?![\x80-\xBF]))|(?![\x80-\xBF]{2})|[\x80-\xBF]{3,})/S',
         '*', $string );
    
        //reject overly long 3 byte sequences and UTF-16 surrogates and replace with ?
        $string = preg_replace('/\xE0[\x80-\x9F][\x80-\xBF]'.
         '|\xED[\xA0-\xBF][\x80-\xBF]/S','?', $string );
    
        return $string;
    }
    

    有关 json_last_error() 的更多信息,请访问源代码! http://php.net/manual/en/function.json-last-error.php

    【讨论】:

      【解决方案3】:

      使用 JSON Builder 真的很容易:Simple JSON for PHP

      include('includes/json.php');
      
      $Json = new json();
      
      $Json->add('status', '200');
      $Json->add('message', 'Success');
      $Json->add('query', 'sql');
      $Json->add("headers",$headers);
      $Json->add("data",$results);
      
      $Json->send();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-03-20
        • 1970-01-01
        • 1970-01-01
        • 2016-06-02
        • 1970-01-01
        • 2022-06-19
        • 2013-02-10
        • 1970-01-01
        相关资源
        最近更新 更多