PHP not writing all keys to array from MySQL Query

PHP not writing all keys to array from MySQL Query

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.
The following is a raw output of one of the JSON Objects, contained within
a JSON Array. 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.
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): },
I've been creating an array of records with this function:
public function getRowArray($mysql_query_result){
$result=array();
while($row=mysql_fetch_array($mysql_query_result)){
$result[]=$row;
}
return $result;
}
And the query result comes from this block, with variables which are
pre-escaped with mysql_real_escape_string:
$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>'$last_sync'
ORDER BY tRelations.ID ASC;") or die(mysql_error());
if(mysql_num_rows($ary)>0){
$array2= $this->getRowArray($ary);
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?