• Home
  • About
  • Contact Us
  • Gallery
  • Video
  • Archives
  • Site Map
Grab the RSS feed

FlashBannerOnline

FlashBannerOnline
  • Home
  • FlashBannerOnline
  • Free Downloads
  • General
  • Tutorials
    • Adobe AIR
    • CSS
    • Fireworks
    • Flash
      • ActionScript
    • FLEX
    • Javascript
      • JQuery
      • MooTools
    • PHP

Convert MYSQL to JSON by PHP

  • PHP, Tutorials

    Posted on October 9th, 2009

    Written by Behrouz Pooladrag

    Related Posts

    • Using URLLoader to send and load Data to Server Used FLEX
    • Understanding OOP – Introduction
    • Flash Optimization – Freeing Memory

    Tags

    "polygon to json" google api map, awk convert mysql to json, conver mysql to json, convert a mysql dump to json, convert from mysql to json, convert json data to mysql sed, convert json sql, convert json to mysql, convert json to mysql using python, convert json to select mysql, convert json to sql, convert json value by query on mysql, convert mysql array to json, convert mysql into json data, convert mysql polygon to json, convert mysql query to json, convert mysql query to json using .net example, convert mysql rowset to json, convert mysql t0o json, convert mysql table json, convert mysql to jason, convert mysql to json, convert mysql to json boolean, convert mysql to json php rest, convert mysql to json php using reset, convert php json, convert php mysql response to json, convert plist to json mysql, convert result query mysql by java to json format, converting mysql array to json, converting mysql full db in json format, converting mysql to json, converting mysql to json, display, convertir mysql json, dump mysql in json, dump mysql to json, encode mysql query into json, encode mysql query using json in codeigniter, encode_json mysql, export data from mysql to json, export mysql as json, export mysql db as json, export mysql php json, export mysql query json, export mysql query results json, export mysql to json, export mysql to json windows, feed mysql data to json, flex json php mysql, gráficos javascript mysql#sclient=psy, how to convert array from mysql to json, how to convert mysql data to json, how to convert mysql table to json with php pdf, how use json_decode in mysql query runtime, import json mysql, import json to mysql, import mysql to json, java convert query mysql to json, jquery json parse mysql query, JSON, json and mysql, json decode in a mysql query, json encode decode mysql, json encode mysql result flash, json from mysql json_decode, json mysql, json mysql parse procedure, json mysql procedure formatting, json mysql tutorial, json object from mysql stored procedure jsp, json php mysql, json serialize,myquery,mysql, json to sql convert, json view mysql, json_decode after mysql, json_decode to mysql, json_decode with mysql, json_encode examples mysql, json_encode mysql, json_encode mysql typ, json_encode online, json_encode text mysql, Mysql, mysql convert json, mysql convert sql to json, mysql convert to json, mysql dans json_encode, mysql dump as json, mysql dump json, mysql et json_encode, mysql export json, mysql export to json, mysql fetch array json, mysql fetcharray convert to json, mysql format as json, mysql function convert to json, mysql function json convert, mysql import json, mysql import json jdbc, mysql insert to json converter, mysql json converter, mysql json decode, mysql json encode, mysql json parse, mysql json tutorial, mysql json_decode equivalent, mysql json_encode dreamweaver, mysql json_parse, mysql parse json, mysql parse json django, mysql parse json_decode, mysql query decrypt json object, mysql query json_encode dojo datagrid, mysql query json_encode($myarray);, mysql query with json_decode, mysql recursive json, mysql recursive json_encode, mysql stored procedure convert json, mysql text, convert to json, mysql to json, mysql to json convert, mysql to json converter, mysql to json export, mysql to json format, mysql to json php, mysql 转json, mysql_query json encode, PHP, php "json to mysql", php encode_json mysql result, php flash json, php json tutorial mysql, php mysql json, php mysql json example, php mysql json export, php mysql json_encode column rename, polygontojson, rails convert mysql result to json, send json to mysql stored procedures, serialize mysql json, storedprocedure for json, tot, transform mysql call to json, transform mysql table json, เปลี่ยน json to mysql
    Convert MYSQL to JSON by PHP

    I did not find any function to convert the result of a MySQL query directly into JSON notation, so I made my own function.

    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.

    I did not find any function to convert the result of a MySQL query directly into JSON notation, so I made my own function.
    This entry was posted on Friday, October 9th, 2009 at 9:08 am and is filed under PHP, Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

    You might also like

    Using URLLoader to send and load Data to Server Used FLEX Here is a very simple example of two way communication with database using Flex and PHP. In this example, we are...
    Date Class in AS3 This tutorial will teach you how to use the Date Class in AS3 to retrieve all information related to time (includes...
    Compass Effect in AS3 We show how to create a draggable compass in AS3. We use tweening as our primary tool. There is a number of trigonometric...
    Applying Flash Filters Using AS3 Filters are readymade effects which you can apply to all visual objects in Flash. This tutorial will teach you...
  • 6 Comments

    Take a look at some of the responses we've had to this article.

    1. mars
      Posted on February 19th

      how would i integrate this in a php file? just add the db connection?

    2. mars
      Posted on February 19th

      what i mean, cause i just saw its unclear, this returns $json string…now how do i print this to screen….

    3. mars
      Posted on February 19th

      and what vars do i have to change in it…sorry, im new to php and json. ive been trying to print the results from a db for hours and cant get it to work, i can however get them into xml format to print…

    4. Behrouz Pooladrag
      Posted on February 20th
      
      $name = "test";
      $mysql_result = mysql_query("your sql code....");
      echo mysql2json($mysql_result,$name);
      
    5. Maxime Lambert
      Posted on June 15th

      Thank you! Great!

    6. Behrouz Pooladrag
      Posted on June 16th

      you’re welcome ;)

  • Post a Comment

    Let us know what you thought.

  • Name:

    Email (required):

    Website:

    Message:

  • Ad Ad

  • FlashBannerOnline.Com

  • Subscribe

    Enter your email address:

    | More

  • Blogroll
    • Random Flash Banner!
  • Tag Cloud

    • actionscript addChild addEventListener Adobe AIR Air Animation AS2 AS3 as3 preloader Banner beginFill BitmapData blog.flashbanneronline.com Class CSS Date Class draw Flash flash.net.URLLoader flash banner free flash banner generator flash banner happy new year flashbanneronline FlashBannerOnline.com flash banner online free flash player function game getChildIndex Javascript lightbox Loading Mysql new year flash banner Object Optimization package PHP Sprite tot Tweener URLLoader URLRequest web XML
  • Recent Comment
    • Behrouz Pooladrag on Flash Optimization – Freezing And Unfreezing Objects
    • Valerie on Flash Optimization – Freezing And Unfreezing Objects
    • FlashLord on RSS Reader for FlashBannerOnline Blog!
    • Behrouz Pooladrag on Using ActionScript 3.0 with PHP Loading External Variables
    • LUN on Using ActionScript 3.0 with PHP Loading External Variables
  • RSS FlashBannerOnline
    • Happy New Year 2012 & Gift!
    • New Templates Oval Light Added to Flash Banner Online
    • FlashBannerOnline started in Arabic language
    • Happy New Year 2011
    • New Templates Christmas 2011 Added at Christmas!
    • New Templates angel animate Added to Flash Banner Online
    • Added Tow New Preloader (Pro Preloader , Rotate Preloader)
    • Happy New Year , by new Theme (Christmas 2010)
  • Add to Technorati Favorites

    Subscribe
  • Google
    Custom Search

Copyright © 2009 FlashBannerOnline - Powered by Wordpress.

FBO by