You have written a PHP result set to convert it to a function in JSON format. You can directly call:
Copy codeThe Code is as follows:
Function RecordToJson ($ recordset)
{
$ Jstr = '[';
While ($ rs = $ recordset-> Fetch ())
{
// $ Nick = iconv ("GBK", 'utf-8', $ rs ['Nick ']);/* convert to UTF-8 encoding */
// TODO: traverses the result set.
$ Arr_keys = array_keys ($ rs );
$ Jstr = $ jstr .'{';
For ($ I = 0; $ I <count ($ arr_keys); $ I + = 2)
{
// The database encoding is gbk and the encoding needs to be converted
// 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 default result set of PHP has a numeric index. The following function can remove the numeric index and only retain the field index:
Copy codeThe Code is as follows:
Function RebuilderRecord ($ recordset)
{
$ Row = 0;
While ($ rs = $ recordset-> Fetch ())
{
// TODO: traverses the 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;
}