Friday, July 7th, 2006

PHP-Based MySQL-to-JSON Converter

Category: Database, Remoting

A new PHP component by Adnan Siddiqi accepts a MySQL result set and converts it into a JSON message. MySQL To JSON:

This class can be used to convert data from MySQL query results into a JavaScript expression in JavaScript Object Notation.

It takes a MySQL query result handle and retrieves the query result column names and the query result data.

The class generates the definition of a JavaScript object in JSON that contains an array of a rows of query result data.

Each array element represents an object with the properties set to the query result column names. The property values are the query results for the respective row and column.

Posted by Michael Mahemoff at 7:23 am

3.5 rating from 95 votes


Comments feed TrackBack URI

never had a problem there… combine Sajax with PEAR_DB, and you can simply:

function getstuff(){
global $db;
return $db->getAll(‘insert sql string here’);

Comment by Kae Verens — July 7, 2006

yea this really isn’t news. mybic or any other ajax framework can already do this without having to add anything else.

Comment by Jim — July 7, 2006

Just because a library already exists where one solution is employed, don’t dismiss this as being less “newsworthy”. More public resources eqals more options, and more examples of other people’s approaches.

Comment by Mike Ritchie — July 7, 2006

Hi All

First of all Iwould like to thank Michael Mahemoff for mentioning my script.I also thank all people who made comments and rated the class.

Offcourse such feature would be available in different frameworks but I think most of people do not like to setup entire framework for a task of one or two web pages.My class could be useful for such small tasks as well as could be used a complete AJAXised application.

Comment by Adnan Siddiqi — July 7, 2006

Yes, that’s a great idea to do this. I have included nearly the same in my own class some weeks ago. you can ouput json or xml. You only need a (up to) 2-dimensional array or a mysql result, the rest does the script. problem: different encoding of database and page. to not forget this, you can define the encodings.
look 6 articles below this for “uniAjax”, sorry for ad – but when I read this, I thought “this is exactly what I’ve done and published” recently.

Comment by Andi — July 8, 2006

Yeah, good to have more choice :)

Comment by Kashif Aziz — July 9, 2006

I am getting javascript error.
Expected “;” at line 44 in jsontest2.html

Comment by Ganesh — January 13, 2007

I am getting javascript error too. I have json.js from

Genereted by OPERA ;
JavaScript – http://localhost/js/json/jsontest2.html
Unknown thread
name: SyntaxError
message: parseJSON

genereted by IE ;
Exception thrown and not caught

Could you please give me some tips?

Thx, haloki

Comment by HALOKI — January 28, 2007

Excellent web site I will be visiting often

Comment by url redirect — July 13, 2007


Comment by jeric — June 11, 2009

Leave a comment

You must be logged in to post a comment.