本文实例讲述了php面象对象数据库操作类。分享给大家供大家参考。 具体实现代码如下: 复制代码 代码如下://此处构造一个数据库操作类,封装所有数据库操作 //可以扩展便于后台管理程序的使用 Class MySQLDB { var $host; var $user; var $passwd; var $database; var $conn;
//利用构造函数实现变量初始化 //同时连接数据库操作 function MySQLDB($host,$user,$password,$database) { $this->host = $host; $this->user = $user; $this->passwd = $password; $this->database = $database; $this->conn=mysql_connect($this->host, $this->user,$this->passwd) or die("Could not connect to $this->host"); mysql_select_db($this->database,$this->conn) or die("Could not switch to database $this->database"); }
//该函数用来关闭数据库连接 function Close() { MySQL_close($this->conn); }
//该函数实现数据库查询操作 function Query($queryStr) { $res =Mysql_query($queryStr, $this->conn) or die("Could not query database"); return $res; }