<?php
include 'conn.php';
$sql ="select * from user"; //SQL
$result =mysql_query($sql);//執(zhí)行SQL
$json ="";
$data =array(); //定義好一個數(shù)組.PHP中array相當(dāng)于一個數(shù)據(jù)字典.
//定義一個類,用到存放從數(shù)據(jù)庫中取出的數(shù)據(jù).
class User
{
public
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);//把數(shù)據(jù)轉(zhuǎn)換為JSON數(shù)據(jù).
echo "{".'"user"'.":".$json."}";
?>
結(jié)果:
{"user":[{"name":"king","id":"1"},{"name":"mm","id":"23"},{"name":"232r","id":"43"},{"name":"8gb","id":"63"},{"name":"sdl","id":"71"},{"name":"5ff","id":"76"},{"name":"ddt","id":"232"},{"name":"3DD","id":"234"},{"name":"43dd","id":"332"},{"name":"674","id":"445"},{"name":"safd3sdf","id":"523"},{"name":"nnn","id":"873"},{"name":"linsdf1","id":"3921"},{"name":"ODSI","id":"6734"},{"name":"45342","id":"6756"},{"name":"tyr","id":"7865"},{"name":"sasdf","id":"8231"},{"name":"linok","id":"8364"},{"name":"hlele","id":"8372"},{"name":"helloking","id":"8392"},{"name":"lidfn","id":"8763"},{"name":"wfasdf03","id":"12321"},{"name":"3423sd","id":"45312"},{"name":"safd3sdf","id":"564342"},{"name":"sdflaso","id":"823123"},{"name":"ssaaa","id":"823131"},{"name":"safd3sdf","id":"5643421"}]}
|
|