<?php
header(
"Content-type:application/json;charset=utf-8"
);
$servername
=
"localhost"
;
$username
=
"root"
;
$password
=
"root"
;
$dbname
=
"test"
;
class
User {
public
$id
;
public
$name
;
public
$age
;
public
$sex
;
}
function
decodeUnicode(
$str
){
return
preg_replace_callback('/\\\\u([0-9a-f]{4})/i',
create_function(
'
$matches
',
'
return
mb_convert_encoding(pack(
"H*"
,
$matches
[1]),
"UTF-8"
,
"UCS-2BE"
);'
),
$str
);
}
$data
=
array
();
$con
=mysqli_connect(
$servername
,
$username
,
$password
,
$dbname
);
$sql
=
"SELECT * FROM `user` WHERE 1"
;
$result
= mysqli_query(
$con
,
$sql
);
if
(
$result
){
while
(
$row
=mysqli_fetch_array(
$result
,MYSQL_ASSOC)){
$user
=
new
User();
$user
->id =
$row
[
"id"
];
$user
->name =
$row
[
"name"
];
$user
->age =
$row
[
"age"
];
$user
->sex =
$row
[
"sex"
];
$data
[]=
$user
;
}
$json
= json_encode(
$data
);
echo
decodeUnicode(
$json
);
}
else
{
echo
"查询失败"
;
}
mysqli_close(
$con
);
?>