2

I have the following PHP code:

$data=mysql_query("SELECT * FROM notes WHERE rootNoteId='$noteId'");
$mainArray;
while($result = mysql_fetch_array($data))
{
    $mainArray[]=$result;
}

$sendback = array(
     "mainArray" => $mainArray
);
sendResponse(200, json_encode($sendback));

My table 'notes' has the following fields:

'noteId'
'authorName'
'noteBody'

However my return JSON string has the following format:

{
    "0": "3",
    "1": "Moe Bit",
    "2": "Sub sub ",
    "noteId": "3",
    "authorName": "Moe Bit",
    "noteBody": "Sub sub "
}

Why is it adding 0,1,2 indexes for the array with duplicate values of my table fields? I just want noteId, authorName, and noteBody-I'm not sure where it's coming up with "0","1","2".

2 Answers 2

2

mysql_fetch_array() in it's default "mode" fetches the result as associative & numeric array. So you get field names (what you want) and numeric indexes (the numbers you don't want).

To solve this pass the constant "MYSQL_ASSOC" as the second parameter to "mysql_fetch_array" or use the mysql_fetch_assoc() function.

Sign up to request clarification or add additional context in comments.

Comments

1

try either mysql_fetch_assoc or mysql_fetch_object

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.