0

I have a problem to echo more than 1000 fields of MySQL records in PHP. I want them to be encoded with JSON. I have 2 table, each of them has 4 columns. The first table named 'contact_anggablokj6', it has 243 rows (number of fields: 4x243=972 fields). The second table, named 'contact_luthfir272' has 448 rows (number of fields: 4x448=1792 fields). At first I got a warning like the one in this post: Warning: a form on this page has more than 1000 fields PHP MySql. I tried the solution in that post, I got no warning anymore, but still I can not echo more than 1000 fields. I made 2 PHP codes, the first one looks like this:

<?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);
?>

The result is correct, all the JSON objects are inside a JSON Array.

But if the value of variable '$user_sxyzID' is changed to 'luthfir272@sxyz.com' which relates to table 'contact_luthfir272' that has 1700's fields, the result shows nothing (there is no error or warning, too). And then I made the second PHP code, it looks like this:

<?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);
?>

It works for both tables, but the result is only JSON Objects, that's the problem.

What should I do to get the result like 'anggablokj6@sxyz.com' user ID (all the JSON objects are inside a JSON Array) for 'luthfir272@sxyz.com' user ID?

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

I modified the PHP code to be like this:

<?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.";
}

The result shows a line of the last row in MySQL table and returns JSON_ERROR_NONE value.(A little progress)

Community
  • 1
  • 1
Luthfi Rahman
  • 424
  • 6
  • 17
  • 5
    You have a new table per user? Why?! Why couldn't you just have 1 table and a field called 'owner', which is the username? – Farkie Feb 22 '16 at 08:36
  • 2
    Hello Sir! In this restaurant tonight we got 60 tables and 60 guests, so I'm afraid we are fully booked. – isnisn Feb 22 '16 at 08:38
  • json_encode breaks if there are characters which are not encoded in utf-8 – undefined_variable Feb 22 '16 at 08:41
  • Not sure if this is the total solution but move the `echo json_encode(array("result"=>$arr_retrieve[$i]));` outside the `while` loop and code it as `echo json_encode(array("result"=>$arr_retrieve));` this will send one JSON String to the browser rather that 1700 little strings – RiggsFolly Feb 22 '16 at 08:46
  • Farkie- I know my mysql design is not pretty, but that's not the point of my problem – Luthfi Rahman Feb 22 '16 at 08:48
  • Also `max_input_vars` controls the number of variables PHP will accept from a submitted form not how much data you can send to a browser. So that may be relevant to getting this data back from the browser but its not relevant to sending data to the browser – RiggsFolly Feb 22 '16 at 08:48
  • isnisn-please do not spam in my post, thank you. – Luthfi Rahman Feb 22 '16 at 08:49
  • @RiggsFolly-I tried it, but still nothing shows up. And for max_input_vars in ini_set() do not help me at all? I will remove it from my code. Thank you so much for mentioning that. – Luthfi Rahman Feb 22 '16 at 08:53
  • @Undefined_variable - I've made sure that all the data is encoded in utf-8 – Luthfi Rahman Feb 22 '16 at 08:54
  • When you increase `max_input_vars` you normally also have to increase `post_max_size` so there is enough room for all these input variables in the input buffer. [See this answer](http://stackoverflow.com/questions/34653295/php-not-inserting-more-then-91-rows-form-data-to-data-base-using-ajax/34654056#34654056) – RiggsFolly Feb 22 '16 at 08:55
  • @RiggsFolly - I increased 'post_max_size' to '16M', but it still not solve my problem (I already restarted the apache). – Luthfi Rahman Feb 22 '16 at 09:01
  • Does this data get to the browser? And what do you do with it when it gets there? – RiggsFolly Feb 22 '16 at 09:12
  • @RiggsFolly - Actually I'm gonna pass this data into an android application. Getting this data to the browser is only for testing purpose. – Luthfi Rahman Feb 22 '16 at 09:24
  • "there is no error or warning" - You don't have code to retrieve it and display it. – Álvaro González Feb 22 '16 at 10:39
  • @ÁlvaroGonzález - I tried to use this code 'error_reporting(E_ALL);', but still there's no any warning or error. – Luthfi Rahman Feb 22 '16 at 10:41
  • You need to verify the actual output of `json_encode()` (not just echo it as string) and call `json_last_error()` when something fails. – Álvaro González Feb 22 '16 at 11:14
  • @ÁlvaroGonzález I tried it and it returns JSON_ERROR_NONE value. I will update the code in my post with this json_last_error addition. – Luthfi Rahman Feb 22 '16 at 11:27
  • 1
    In the updated code you have to put the `$arr_retrieve = array();` outside the loop, otherwise you will always end up with just the last row. – Aioros Feb 22 '16 at 11:39
  • Try writing the JSON encoded string to a file instead of echoing to screen, see if the file is empty or it cuts off at a certain point – WebChemist Feb 22 '16 at 11:41
  • @Aioros - I tried your suggestion and the result is only show this"[];", the last row data disappear. I put "$arr_retrieve=array();" just before echoing that array. – Luthfi Rahman Feb 22 '16 at 11:44
  • Well, yes, that line assigns an empty array to `$arr_retrieve`: it needs to be before the loop. – Aioros Feb 22 '16 at 11:46
  • @Aioros - I updated the code, I put the $arr_retrieve initialization just before the if() statement, and the result is still the same as before, only showing the last row of the MySQL table. – Luthfi Rahman Feb 22 '16 at 11:53
  • Ok, but the updated code in the question still has the same line inside the loop, I suggest correcting it – Aioros Feb 22 '16 at 11:58
  • @Aioros - I removed the $arr_retrieve initialization from the loop and I got the JSON_ERROR_UTF8 return value from the json_last_error(). Should I check the records in MySQL table to troubleshoot this problem? – Luthfi Rahman Feb 22 '16 at 12:02
  • @Aioros - Thank you so much, I check my table and now I can get all the records from MySQL table. – Luthfi Rahman Feb 22 '16 at 12:16
  • @Undefined_variable - You're right, I'm sorry there's something I missed when verify the data to be encoded in UTF-8. – Luthfi Rahman Feb 22 '16 at 12:17

1 Answers1

0

The main problem is my PHP code didn't verify the MySQL records to be encoded in UTF-8. The json_encode() will break down if there's a data that doesn't encoded in UTF-8. So as suggested by Undefined_variable and Aioros, make sure all the data encoded in UTF-8 to use json_encode(). This is the correct script that will tell you if there is something wrong when using 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);
?>

Hope it is useful for others. Thank you everyone for helping me out.

Luthfi Rahman
  • 424
  • 6
  • 17