/ Published in: PHP
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
function convertResult($rs, $type, $jsonmain="") { // receive a recordset and convert it to csv // or to json based on "type" parameter. $csvString = ""; $csvcolumns = ""; $count = 0; while($r = $rs->fetch_row()) { $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++; } return ($type=="csv"?$csvdata:$jsondata); }
URL: http://www.barattalo.it/2010/01/29/10-php-usefull-functions-for-mysqli-improved-stuff/