Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP not writing all keys to array from MySQL Query
    primarykey
    data
    text
    <p>I'm having an odd time writing all of the field names as keys to an array from a MySQL Query in PHP. All of the values are making it from the query to the array, but not all of the keys. I'm encoding the response in JSON and sending it to an Android client.</p> <p>The following is a raw output of one of the <code>JSON Objects</code>, contained within a <code>JSON Array</code>. As you can see, many of the keys are just numbers. The order of the numbers doesn't line up with the original query either.</p> <pre><code>08-18 05:41:49.130: I/GETJSON=ACCESSOR(JSON)(20407): { 08-18 05:41:49.130: I/GETJSON=ACCESSOR(JSON)(20407): "STATE": "1", 08-18 05:41:49.130: I/GETJSON=ACCESSOR(JSON)(20407): "RECEIVER_ID": "29", 08-18 05:41:49.130: I/GETJSON=ACCESSOR(JSON)(20407): "LAST_MODIFIED": "2013-08-17 06:15:01", 08-18 05:41:49.130: I/GETJSON=ACCESSOR(JSON)(20407): "CREATED": "2013-08-07 15:51:25", 08-18 05:41:49.130: I/GETJSON=ACCESSOR(JSON)(20407): "UNIQUE_ID": "cef3fc71-073e-11e3-8ffd-0800200c9a66", 08-18 05:41:49.130: I/GETJSON=ACCESSOR(JSON)(20407): "3": "2", 08-18 05:41:49.130: I/GETJSON=ACCESSOR(JSON)(20407): "2": "29", 08-18 05:41:49.130: I/GETJSON=ACCESSOR(JSON)(20407): "1": "918fa7f5-073c-11e3-8ffd- 0800200c9a66", 08-18 05:41:49.130: I/GETJSON=ACCESSOR(JSON)(20407): "0": "2", 08-18 05:41:49.130: I/GETJSON=ACCESSOR(JSON)(20407): "7": "2013-08-07 15:51:25", 08-18 05:41:49.130: I/GETJSON=ACCESSOR(JSON)(20407): "6": "2013-08-17 06:15:01", 08-18 05:41:49.130: I/GETJSON=ACCESSOR(JSON)(20407): "DATA_RECORD_UUID": "918fa7f5-073c-11e3-8ffd-0800200c9a66", 08-18 05:41:49.130: I/GETJSON=ACCESSOR(JSON)(20407): "5": "cef3fc71-073e-11e3-8ffd-0800200c9a66", 08-18 05:41:49.130: I/GETJSON=ACCESSOR(JSON)(20407): "4": "1", 08-18 05:41:49.130: I/GETJSON=ACCESSOR(JSON)(20407): "ID": "2", 08-18 05:41:49.130: I/GETJSON=ACCESSOR(JSON)(20407): "IS_TEST": "2" 08-18 05:41:49.130: I/GETJSON=ACCESSOR(JSON)(20407): }, </code></pre> <p>I've been creating an array of records with this function:</p> <pre><code> public function getRowArray($mysql_query_result){ $result=array(); while($row=mysql_fetch_array($mysql_query_result)){ $result[]=$row; } return $result; } </code></pre> <p>And the query result comes from this block, with variables which are pre-escaped with mysql_real_escape_string:</p> <pre><code>$ary=mysql_query("SELECT tRelations.ID,tRelations.DATA_RECORD_UUID,tRelations.RECEIVER_ID, tRelations.IS_TEST,tRelations.STATE,tRelations.UNIQUE_ID,tRelations.LAST_MODIFIED, tRelations.CREATED FROM tRelations JOIN tUserData ON (tUserData.UNIQUE_ID=tRelations.DATA_RECORD_UUID) JOIN tUsers ON (tUsers.ID=tUserData.USER_ID) WHERE tUsers.ID=$user_id AND tRelations.last_modified&gt;'$last_sync' ORDER BY tRelations.ID ASC;") or die(mysql_error()); if(mysql_num_rows($ary)&gt;0){ $array2= $this-&gt;getRowArray($ary); </code></pre> <p>Might anybody know why the keys are out of order and not all being written? Alternatively, does anybody else favour other methods of converting a MySQL result into a JSON array?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload