Return to Snippet

Revision: 16647
at August 11, 2009 03:11 by hasantayyar


Initial Code
<?
/** 
* Filename: mysql2json.class.php 
* Purpose: Convert mysql resultset data into JSON(http://json.org) format
* Author: Adnan Siddiqi <[email protected]> 
* License: PHP License 
* Date: Tuesday,June 21, 2006 
*
*/


class mysql2json{

 function getJSON($resultSet,$affectedRecords){
 $numberRows=0;
 $arrfieldName=array();
 $i=0;
 $json="";
    //print("Test");
     while ($i < mysql_num_fields($resultSet))  {
         $meta = mysql_fetch_field($resultSet, $i);
        if (!$meta) {
        }else{
        $arrfieldName[$i]=$meta->name;
        }
        $i++;
     }
     $i=0;
      $json="{\n\"data\": [\n";
    while($row=mysql_fetch_array($resultSet, MYSQL_NUM)) {
        $i++;
        //print("Ind ".$i."-$affectedRecords<br>");
        $json.="{\n";
        for($r=0;$r < count($arrfieldName);$r++) {
            $json.="\"$arrfieldName[$r]\" :    \"$row[$r]\"";
            if($r < count($arrfieldName)-1){
                $json.=",\n";
            }else{
                $json.="\n";
            }
        }
        
        
         if($i!=$affectedRecords){
             $json.="\n},\n";
         }else{
             $json.="\n}\n";
         }
         
         
        
    }
    $json.="]\n};";
    
    return $json;
 }


}
?>

Initial URL
http://www.phpclasses.org/browse/file/14803.html

Initial Description
mysql2json.class.php

Initial Title
MySQL to JSON - PHP Class

Initial Tags
json

Initial Language
PHP