您的位置:首页 > 编程语言 > PHP开发

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

2013-07-22 21:28 711 查看
<?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;
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: