Revision: 23175
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at February 1, 2010 05:45 by ginoplusio
Initial Code
function convertResult($rs, $type, $jsonmain="") { // receive a recordset and convert it to csv // or to json based on "type" parameter. $jsonArray = array(); $csvString = ""; $csvcolumns = ""; $count = 0; while($r = $rs->fetch_row()) { for($k = 0; $k < count($r); $k++) { $rs->field_seek($k); $finfo = $rs->fetch_field(); $jsonArray[$count][$finfo->name] = $r[$k]; $csvString.=",\"".$r[$k]."\""; if (!$count) $csvcolumns.=($csvcolumns?",":"").$finfo->name; } $csvString.="\n"; $count++; } $jsondata = "{\"$jsonmain\":".json_encode($jsonArray)."}"; $csvdata = str_replace("\n,","\n",$csvcolumns."\n".$csvString); return ($type=="csv"?$csvdata:$jsondata); }
Initial URL
http://www.barattalo.it/2010/01/29/10-php-usefull-functions-for-mysqli-improved-stuff/
Initial Description
Initial Title
MySQLi convert a result set to CSV or to JSON
Initial Tags
mysql, php, csv, json
Initial Language
PHP