<?php
header ( "Content-type:text/html;charset=utf-8" ); //统一输出编码为utf-8
/* Connect to a MySQL server 连接数据库服务器 */
$link = mysqli_connect(
'localhost', /* The host to connect to 连接MySQL地址 */
'root', /* The user to connect as 连接MySQL用户名 */
'', /* The password to use 连接MySQL密码 */
'test'); /* The default database to query 连接数据库名称*/
if (!$link) {
printf("Can't connect to MySQL Server. Errorcode: %s ", mysqli_connect_error());
exit;
}
mysqli_query($link,'set names utf8'); //设置读取数据后的编码
$result = mysqli_query($link, 'SELECT * FROM t1');
// 创建数组,存放数据库的数据并传递出去
$list = array();
while( $row = mysqli_fetch_assoc($result) )
{
$list[] = $row;//将数据库里面的每一行数据存到list数组里面去
}
echo json_encode($list);
/* Close the connection 关闭连接*/
mysqli_close($link);
?>