1

From my experience, when I json_encode data from a mysql table, the output is an object containing each row as an object within. What I want to do is to get each row as an array, like the example below (don't need the column names in there). How can I do that?

{ "Data": [
    ["1","Internet Explorer 4.0","Win 95+","4","X","1"],
    ["2","Internet Explorer 5.0","Win 95+","5","C","2"],
    ["3","Internet Explorer 5.5","Win 95+","5.5","A","3"],
    ["4","Internet Explorer 6","Win 98+","6","A","4"],
] }

2 Answers 2

4

Use mysql_fetch_row [docs] to get each row. This will get a row as a numerical array and those are also encoded as JSON arrays:

$data = array();

while(($row = mysql_fetch_row($result))) {
    $data[] = $row;
}

echo json_encode(array('Data' => $data));
Sign up to request clarification or add additional context in comments.

3 Comments

Where is $result defined?
Where is $result defined? [2]
@FlipNovid: $result is the result of running a query. If you look at the documentation I linked to you can see an example.
0

NOT CHECKED in interpreter: You can change the type of variable by simple types convertion:

$data = DB::get('SELECT * FROM `table` WHERE 1');
echo json_encode(array('Data' => (array)$data));

Also you can use next function: array mysql_fetch_array ( resource $result [, int $result_type ] )

1 Comment

DB abstraction. You can use it as $data = mysql_fetch_array($mysql_result, MYSQL_NUM); Just convert object to array. It must help.

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.