How do I use the json_encode()
function with MySQL query results? Do I need to iterate through the rows or can I just apply it to the entire results object?
相关问题
- Views base64 encoded blob in HTML with PHP
- Jackson Deserialization not calling deserialize on
- Laravel Option Select - Default Issue
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
we could simplify Paolo Bergantino answer like this
The function
json_encode
needs PHP >= 5.2 and the php-json package - as mentioned hereNOTE:
mysql
is deprecated as of PHP 5.5.0, usemysqli
extension instead http://php.net/manual/en/migration55.deprecated.php.Sorry, this is extremely long after the question, but:
Just basically:
aarray_push($rows,$row_array); help to build array otherwise it give last value in the while loop
this work like append method of StringBuilder in java
One more option using FOR loop:
The only disadvantage is that loop for is slower then e.g. while or especially foreach
Check the code below for using mysql_fetch and json_encode. You will need to iterate through the rows but if you use mysqli the situation will change