爱程序网

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

来源: 阅读:

<?phpclass 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;?>

相关文章列表: