PHP JSON_ENCODE无法显示超过1000个MySQL表字段

时间:2016-02-22 08:33:53

标签: php mysql json

我有一个问题是在PHP中回显1000多个MySQL记录字段。我希望它们用JSON编码。我有2个表,每个表有4列。第一个表名为“contact_anggablokj6”,它有243行(字段数:4x243 = 972个字段)。名为“contact_luthfir272”的第二个表有448行(字段数:4x448 = 1792个字段)。起初我收到了一条警告,就像这篇文章中的那个:Warning: a form on this page has more than 1000 fields PHP MySql。我在那篇文章中尝试了解决方案,我没有得到任何警告,但仍然无法回显超过1000个字段。我制作了2个PHP代码,第一个看起来像这样:

<?php
include ('db_connect.php');
ini_set('max_input_vars', 5000);

$user_sxyzID='anggablokj6@sxyz.com';
$j = strpos($user_sxyzID,"@");
$trimmed_sxyzid = substr($user_sxyzID, 0, $j);

$arr_retrieve = array();

$sql_retrieve="SELECT * FROM contacts_$trimmed_sxyzid";

$retrieved = mysql_query($sql_retrieve);

$i=0;

while($row=mysql_fetch_array($retrieved)){
    $arr_retrieve[$i] = array(
      "contact_id" => $row['contact_ID'],
      "contact_name" => $row['contact_name'],
      "contact_phone" => $row['contact_phone'],
      "contact_email" => $row['contact_email']
    );
    $i++;
}
echo json_encode(array("result"=>$arr_retrieve));

mysql_close($con);
?>

结果是正确的,所有JSON对象都在JSON数组中。

但是如果变量'$ user_sxyzID'的值更改为'luthfir272@sxyz.com',它与具有1700字段的表'contact_luthfir272'相关,则结果不显示任何内容(也没有错误或警告)。然后我制作了第二个PHP代码,它看起来像这样:

<?php
include ('db_connect.php');
ini_set('max_input_vars', 5000);

$user_sxyzID='anggablokj6@sxyz.com';
$j = strpos($user_sxyzID,"@");
$trimmed_sxyzid = substr($user_sxyzID, 0, $j);

$arr_retrieve = array();

$sql_retrieve="SELECT * FROM contacts_$trimmed_sxyzid";

$retrieved = mysql_query($sql_retrieve);

$i=0;

while($row=mysql_fetch_array($retrieved)){
    $arr_retrieve[$i] = array(
      "contact_id" => $row['contact_ID'],
      "contact_name" => $row['contact_name'],
      "contact_phone" => $row['contact_phone'],
      "contact_email" => $row['contact_email']
    );
    echo json_encode(array("result"=>$arr_retrieve[$i]));
    $i++;
}

mysql_close($con);
?>

它适用于两个表,但结果只是JSON对象,这就是问题。

对于'luthfir272@sxyz.com'用户ID,我应该如何获得'anggablokj6@sxyz.com'用户ID(所有JSON对象都在JSON数组中)的结果?

------------------ UPDATE ---------------------------- ----------------------------

我将PHP代码修改为:

<?php
header('Content-type: application/json');
include ('db_connect.php');
ini_set('max_input_vars', 5000);
ini_set('post_max_size', '16M');

$user_sxyzID='luthfir272@sxyz.com';
$j = strpos($user_sxyzID,"@");
$trimmed_sxyzid = substr($user_sxyzID, 0, $j);

$sql_retrieve="SELECT * FROM contacts_$trimmed_sxyzid";

$retrieved = mysql_query($sql_retrieve);

$i=0;

$arr_retrieve = array();

if(mysql_num_rows($retrieved)>0){
 while($row=mysql_fetch_assoc($retrieved)){
    $arr_retrieve[$i] = array(
      "contact_id" => $row['contact_ID'],
      "contact_name" => $row['contact_name'],
      "contact_phone" => $row['contact_phone'],
      "contact_email" => $row['contact_email']
    );         
    $i++;
 }

 echo json_encode($arr_retrieve);

 switch (json_last_error()) {
    case JSON_ERROR_NONE:
        echo ' - No errors';
    break;
    case JSON_ERROR_DEPTH:
        echo ' - Maximum stack depth exceeded';
    break;
    case JSON_ERROR_STATE_MISMATCH:
        echo ' - Underflow or the modes mismatch';
    break;
    case JSON_ERROR_CTRL_CHAR:
        echo ' - Unexpected control character found';
    break;
    case JSON_ERROR_SYNTAX:
        echo ' - Syntax error, malformed JSON';
    break;
    case JSON_ERROR_UTF8:
        echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
    break;
    default:
        echo ' - Unknown error';
    break;
  }
}else{
echo "There's no data found.";
}

结果显示MySQL表中最后一行的一行并返回JSON_ERROR_NONE值。(稍微进步)

1 个答案:

答案 0 :(得分:0)

主要问题是我的PHP代码没有验证要以UTF-8编码的MySQL记录。如果存在未以UTF-8编码的数据,则json_encode()将分解。因此,根据Undefined_variable和Aioros的建议,确保以UTF-8编码的所有数据都使用json_encode()。这是正确的脚本,可以告诉您使用json_encode()时是否有错误。

<?php
//check if there's something wrong with json_last_error():
switch (json_last_error()) {
case JSON_ERROR_NONE:
    echo ' - No errors';
break;
case JSON_ERROR_DEPTH:
    echo ' - Maximum stack depth exceeded';
break;
case JSON_ERROR_STATE_MISMATCH:
    echo ' - Underflow or the modes mismatch';
break;
case JSON_ERROR_CTRL_CHAR:
    echo ' - Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
    echo ' - Syntax error, malformed JSON';
break;
case JSON_ERROR_UTF8:
    echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
break;
default:
    echo ' - Unknown error';
break;
}
}else{
  echo "There's no data found.";
}

mysql_close($con);
?>

希望它对其他人有用。谢谢大家帮助我。