I wrote a PHP result set into the JSON format function, you can call directly:
Copy Code code as follows:
function Recordtojson ($recordset)
{
$jstr = ' [';
while ($rs = $recordset->fetch ())
{
$nick = Iconv ("GBK", ' Utf-8 ', $rs [' Nick ']);/* Convert to UTF-8 encoding * *
TODO: Traverse result set
$arr _keys=array_keys ($RS);
$jstr = $jstr. ' {';
for ($i =0; $i <count ($arr _keys); $i +=2)
{
The database code is GBK and needs to convert the encoding
Todo;iconv ("GBK", ' Utf-8 ', $rs [' Nick ']);/* Convert to UTF-8 encoding * *
$key =iconv ("GBK", ' Utf-8 ', $arr _keys[$i]);//$arr _keys[$i];
$value =iconv ("GBK", ' Utf-8 ', $rs [$arr _keys[$i]]);//$rs [$arr _keys[$i]];
$jstr = $jstr. ' "'. $key. ' ":" $value. ' ", ';
}
$jstr =substr ($jstr, 0,strlen ($JSTR)-1);
$jstr = $jstr. '}, ';
}
$jstr =substr ($jstr, 0,strlen ($JSTR)-1);
$jstr = $jstr. '] ';
return $jstr;
}
The PHP default result set array has a numeric index, and the following function removes the numeric index and retains only the field index:
Copy Code code as follows:
function Rebuilderrecord ($recordset)
{
$row = 0;
while ($rs = $recordset->fetch ())
{
TODO: Traverse result set
$arr _keys=array_keys ($RS);
for ($i =0; $i <count ($arr _keys); $i +=2)
{
$newrs [$row] [$arr _keys[$i]]= $rs [$arr _keys[$i]];
}
$row + +;
}
return $newrs;
}