< ? class mysql { var $host; var $user; var $passwd; var $database; function mysql() //利用构造函数实现变量初始化 { $host = ""; $user = ""; $passwd = ""; $database = ""; } function connect() { $conn = mysql_connect($this->host, $this->user,$this->passwd) or die("could not connect to $this->host"); mysql_select_db($this->database,$conn) or die("could not switch to database $this->database;"); return $conn; } function close($conn) { mysql_close($conn); }
function query($querystr, $conn) { $res =mysql_query($querystr, $conn) or die("could not query database"); return $res; } function getrows($res) { $rowno = 0; $rowno = mysql_num_rows($res); if($rowno>0) { for($row=0;$row<$rowno;$row++) { $rows[$row]=mysql_fetch_row($res); } return $rows; } } function getrowsnum($res) { $rowno = 0; $rowno = mysql_num_rows($res); return $rowno; } } ? > 同样我们要封装其他的“数据库驱动”到我们的sql类中,只需要建立相应的类,并以同名命名驱动文件,放到php的include目录就可以了。