How to Convert PHP Database Result Arrays to JSON?

Linda Hamilton
Release: 2024-10-31 07:11:30
Original
950 people have browsed it

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>
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!