Home > Article > Backend Development > How to convert MySQL result arrays to JSON in PHP (pre-5.2.0)?
Problem:
You want to convert the result array from a MySQL query to JSON format in PHP, preferably with a solution that is compatible with PHP versions below 5.2.0.
Solution:
For PHP versions 5.2.0 and above:
Utilize the json_encode() function:
<code class="php">echo json_encode($row);</code>
For PHP versions below 5.2.0:
Utilize the JSON class from the PEAR package:
pear install JSON
<code class="php">include_once 'JSON.php';</code>
<code class="php">$json = new Services_JSON;</code>
<code class="php">echo $json->encode($row);</code>
The above is the detailed content of How to convert MySQL result arrays to JSON in PHP (pre-5.2.0)?. For more information, please follow other related articles on the PHP Chinese website!