简单的php数据库操作类

简单的数据库类,代码:


<?php

class db{

	public $host;
	public $user;
	public $pwd;
	public $dbname;
	public $table;
	
	function __construct($host,$user,$pwd,$dbname,$encode){
		$this->host = $host;
		$this->user = $user;
		$this->pwd = $pwd;
		$this->dbname = $dbname;
		$this->encode = $encode;
	}
	
	function connect(){
		$conn = mysql_connect($this->host,$this->user,$this->pwd,$this->dbname);
		mysql_set_charset($this->encode);
		mysql_select_db($this->dbname);
		return $conn;
	}
	
	function query($sql,$conn){
		$this->sql = $sql;
		$result = mysql_query($this->sql);
		while($re = mysql_fetch_assoc($result)){
			$array[] = $re;
		}
		return $array;
	}
	
	function __distruct(){
		mysql_free_result();
		mysql_close();
	}
}

$db = new db('localhost','root','','mogu','utf8');
$conn = $db->connect();
$sql = 'select * from city where id="1"';
$result = $db->query($sql,$conn);
var_dump($result);

?>

发表评论