MySQL to JSON with PHP
Ugly title for a post, really… but I did not find any function to convert the result of a MySQL query directly into JSON notation, so I made my own function.
I really needed it today, so I am sharing it with you. It’s not a everyday use function, but I am sure some of you will find it useful, just like it happened with Sending email with multiple attachments with PHP.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | function mysql2json($mysql_result,$name){ $json="{\n\"$name\": [\n"; $field_names = array(); $fields = mysql_num_fields($mysql_result); for($x=0;$x<$fields;$x++){ $field_name = mysql_fetch_field($mysql_result, $x); if($field_name){ $field_names[$x]=$field_name->name; } } $rows = mysql_num_rows($mysql_result); for($x=0;$x<$rows;$x++){ $row = mysql_fetch_array($mysql_result); $json.="{\n"; for($y=0;$y<count($field_names);$y++) { $json.="\"$field_names[$y]\" : \"$row[$y]\""; if($y==count($field_names)-1){ $json.="\n"; } else{ $json.=",\n"; } } if($x==$rows-1){ $json.="\n}\n"; } else{ $json.="\n},\n"; } } $json.="]\n};"; return($json); } |
The first parameter is the result of the query, without any parsing, just the output of mysql_query function, the second parameter is the name you want to give to your JSON object.
Returns a string with the JSON notation of the result.
3 Responses to “MySQL to JSON with PHP”
Leave a Reply
- Get up to $100,000 for your next Flash game with Mochi GAME Developer Fund
- Create a dynamic content animated footer ad for your site in just 9 jQuery lines – 17 lines version
- Sell sitelocked version of your Flash games and even .fla sources to Free Online Games
- Protect your work from ActionScript code theft with SWF Protector
- Create a dynamic content animated footer ad for your site in just 9 jQuery lines
- Understanding Box2D’s one-way platforms, aka CLOUDS
- Triqui MochiAds Arcade plugin for WordPress upgraded to 1.2
- Box2D Flash game creation tutorial – part 2
- 11 Flash isometric engines you can use in your games
- Monetize your Flash games with GamesChart
- Create a Lightbox effect only with CSS - no javascript needed
- Flash game creation tutorial - part 1
- Create a Flash Racing Game Tutorial
- Flash game creation tutorial - part 2
- Make a Flash game like Flash Element Tower Defense - Part 2
- Flash game creation tutorial - part 3
- Make a Flash game like Flash Element Tower Defense - Part 1
- Create a flash draw game like Line Rider or others - part 1
- Triqui MochiAds Arcade plugin for WordPress official page
- Create a flash artillery game - step 1
- Flash game creation tutorial – part 5.2 (4.88/5)
- Create a flash artillery game – step 1 (4.79/5)
- Create a Flash Racing Game Tutorial (4.76/5)
- Create a flash artillery game – step 2 (4.74/5)
- Create a survival horror game in Flash tutorial – part 1 (4.73/5)
- Creation of a Flash arcade site using WordPress – step 2 (4.73/5)
- Flash game creation tutorial – part 2 (4.71/5)
- Flash game creation tutorial – part 1 (4.70/5)
- Create a flash draw game like Line Rider or others – part 1 (4.69/5)
- Creation of a platform game with Flash – step 2 (4.68/5)









Don’t forget AMFPHP’s great JSON gateway. If you have an AMFPHP service e.g. “getMyStuff” a call to the gateway will return a JSON version. Example:
function getMyStuff($usr,$pwd) {
// get the stuff with php from mysql
return $mySqlResult;
}
example JSON call:
http://somesite.com/amfphp/myservices/getMyStuff/myusrname/mypwd
You can also set this up to use POST vars instead of inline GET
One thing I’d suggest would be to add the line $count_names = count($field_names); after the line beginning with $rows = mysql_num… and refer all of your references to count($field_names) instead to the $count_names var.
Otherwise you call the count() function twice for every occurence of $x in each $y, which can add up, especially if you’re querying the number of fields and values in a database.
Otherwise, keep up the great posts, especially with PHP :)
PHP 5.2+ has a function called json_encode(). So you can shorten everything to this:
function mysql2json($mysql_result,$name) {
$result = array($name => array());
while ($row = mysql_fetch_assoc($mysql_result))
$result[$name][] = $row;
return json_encode($result);
}
(I haven’t tested it, but it should work).