<?php
include 'conn.php';
$sql ="select * from user"; //SQL
$result =mysql_query($sql);//执行SQL
$json ="";
$data =array(); //定义好一个数组.PHP中array相当于一个数据字典.
//定义一个类,用到存放从数据库中取出的数据.
class User
{
public
$name ;
public $id ;
}
//
while ($row= mysql_fetch_array($result, MYSQL_ASSOC))
{
$user =new User();
$user->name = $row["name"];
$user->id = $row["id"];
$data[]=$user;
}
$json = json_encode($data);//把数据转换为JSON数据.
echo "{".'"user"'.":".$json."}";
?>