2017-03-03 52 views
0

我正在使用下面的代码生成JSON,在本地WAMP服务器上正常工作。但是,当我将文件导出到WEB时,脚本仅在SELECT中生成带有几个银行的JSON,如果查询大于10个结果,则它不会生成任何内容,而本地服务器上的某些内容不会发生。使用大量行生成PHP的JSON

有没有人有这个问题?编辑PHP.ini文件或者其他的东西会是这种情况吗?

<?php 
    //open connection to mysql db 
    $connection = mysqli_connect("localhost","root","123","vendas") or die("Error " . mysqli_error($connection)); 

    //fetch table rows from mysql db 
    $sql = "SELECT * FROM os WHERE id_user = 15 and data_conclusao = '2017-03-02'"; 
    $result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection)); 

    //create an array 
    $emparray = array(); 
    while($row =mysqli_fetch_assoc($result)) 
    { 
     $emparray[] = $row; 
    } 
    echo json_encode($emparray); 

    //close the db connection 
    mysqli_close($connection); 
?> 

回答

0

检查您的DATAS有UTF-8 caracters。如果是,尝试通过更换代码:

while($row =mysqli_fetch_assoc($result)) { 
    $emparray[] = array_map('utf8_encode', $row); 
} 
0

json_encode是一个非常静音的功能,如果有什么不顺心而编码,它简单地返回,没有错误信息虚假。如果您确定$emparray确实包含所需的数据,那么您的下一个调试步骤是使用json_last_error(),这将帮助您了解此处发生的情况。

此类行为的一个常见原因是输入字符串中的非可编码字符,或者通常是在那里出现二进制紧接。但是,只有当你解码错误信息时,你才能够学习它 - 按照我提供的参考。

0
You could encode each row, one row at a time instead of encoding it in one big operation.  
$db=mysql_connect($host, $username, $password) or die('Could not connect'); 
    mysql_select_db($db_name, $db) or die(''); 

    $result = mysql_query("SELECT * from listinfo") or die('Could not query'); 

    if(mysql_num_rows($result)){ 
     echo '{"testData":['; 

     $first = true; 
     $row=mysql_fetch_assoc($result); 
     while($row=mysql_fetch_row($result)){ 
      // cast results to specific data types 

      if($first) { 
       $first = false; 
      } else { 
       echo ','; 
      } 
      echo json_encode($row); 
     } 
     echo ']}'; 
    } else { 
     echo '[]'; 
    } 

    mysql_close($db); 
+0

蛰códigofuncionou apenas没有formato阙欧盟precisei,欧盟tentei鲷-LO MAIS做阙Ø埃罗。 –

+0

[ \t { “id_equipe”: “100222”, “viatura”: “123”, “nome1”: “23456” }, \t { “id_equipe”: “1022220”, “viatura” :“123”, “nome1”:“23456” \t} ] –