You can directly call a function that converts a PHP result set to JSON format. if you need a PHP result set, you can refer to the function that converts it to JSON format, you can directly call:
The 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
{
// 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:
The 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
{
$ Newrs [$ row] [$ arr_keys [$ I] = $ rs [$ arr_keys [$ I];
}
$ Row ++;
}
Return $ newrs;
}