PHP面向对象之将数据库的查询结果序列化成json格式

<?php
class link_mysql{
private $host,$uid,$pwd,$db,$link,$res;
function link_mysql($_host,$_uid,$_pwd,$_db){
$this->host = $_host;
$this->uid = $_uid;
$this->pwd = $_pwd;
$this->db = $_db;
$this->link = mysql_connect($this->host,$this->uid,$this->pwd);
mysql_select_db($this->db,$this->link);
}
function exec_sql($sql){
//if you use insert sentence,then you must open your link;
$res = mysql_query($sql);
return $res;
}
}
$obj_link = new link_mysql('localhost','***','***','website');
$res = $obj_link->exec_sql("select * from userinfo");
$str = "[";
while($rows = mysql_fetch_assoc($res)){
$str = $str."{name:$rows[name],password:$rows[password],age:$rows[age]},";
}
$str = rtrim($str,',') ."]";
echo $str;
?>
上一篇:UVA大模拟代码(白书训练计划1)UVA 401,10010,10361,537,409,10878,10815,644,10115,424,10106,465,10494


下一篇:文件大小的友好输出及其 Python 实现