分类:
2008-05-29 10:23:12
以下为引用的内容: phperz~com < ? class SQL { var $Driver; //实际操作的数据库驱动子类 var $connection; //共用的数据库连接变量 function DriverRegister($d) { if($d!="") { $include_path = ini_get("include_path"); $DriverFile = $include_path."/".$d.".php"; //驱动的存放路径必须在PHP.ini文件中设定的INCLUDE_PATH下 if( file_exists( $DriverFile)) //查找驱动是否存在 { include($DriverFile); $this->Driver = new $d(); // 根据驱动名称生成相应的数据库驱动类 return true; } } return false; //注册驱动失败 } function Connect($host,$user,$passwd,$database)//连接数据库的函数 { $this->Driver->host=$host; $this->Driver->user=$user; www~phperz~com $this->Driver->passwd=$pas swd; $this->Driver->database=$d atabase; $this->connection = $this->Driver->Connect(); } function Close()//关闭数据库函数 { $this->Driver->close($this->connection); } function Query($queryStr)//数据库字符串查询函数 { return $this->Driver->query($queryStr,$this->connection); } function getRows($res)//查找行 { return $this->Driver->getRows($res); } function getRowsNum($res)//取得行号 { return $this->Driver-> getRowsNum ($res); } } ? > |
以下为引用的内容: < ? 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"); php程序员站 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); phperz.com return $rowno; } } ? > |
以下为引用的内容: < ? Include(“SQL.php”); $sql = new SQL; //生成新的Sql对象 if($sql-> DriverRegister(“MySQL”)) //注册数据库驱动 { $sql->Connect(“localhost”,”root”,””,”test”); $res=$sql->query(“select * from test”); //返回查询记录集 $rowsnum = $sql->getRowsNum($res); if($rowsnum > 0) { $rows = $sql->getRows($res); foreach($rows as $row) //循环取出记录集内容 { foreach($row as $field){ print $field;} } } $sql->Close(); } ? > |