Home  >  Article  >  Backend Development  >  How to Convert PHP Database Result Arrays to JSON?

How to Convert PHP Database Result Arrays to JSON?

Linda Hamilton
Linda HamiltonOriginal
2024-10-31 07:11:30950browse

How to Convert PHP Database Result Arrays to JSON?

Converting PHP Database Result Arrays to JSON

You want to convert the result array of a database query to JSON format in PHP. The following code retrieves a row from the database:

<code class="php">$row = mysql_fetch_array($result);</code>

To convert the $row array to JSON format for use with jQuery plugins, you can utilize the json_encode function:

<code class="php">echo json_encode($row);</code>

Note that json_encode is only available in PHP version 5.2.0 and above. If you are using an earlier PHP version, you may need to use a third-party library or write your own JSON encoding function.

The above is the detailed content of How to Convert PHP Database Result Arrays to JSON?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn