-
-
-
- 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;
- }
-
-
- function getRows($res)
- {
- $rowno = 0;
- $rowno = MySQL_num_rows($res);
- if($rowno>0)
- {
- for($row=0;$row<$rowno;$row++ )
- {
- $rows[$row]=MySQL_fetch_array($res);
-
-
- }
- return $rows;
- }
- }
-
-
- function getRowsNum($res)
- {
- $rowno = 0;
- $rowno = mysql_num_rows($res);
- return $rowno;
- }
-
-
- function getFieldsNum($res)
- {
- $fieldno = 0;
- $fieldno = mysql_num_fields($res);
- return $fieldno;
- }
-
-
- function getFields($res)
- {
- $fno = $this->getFieldsNum($res);
- if($fno>0)
- {
- for($i=0;$i<$fno;$i++ )
- {
- $fs[$i]=MySQL_field_name($res,$i);
- }
- return $fs;
- }
- }
-
- }
-
-
-
- $SqlDB = new MySQLDB("localhost","root","root","testdb");
-
- $sql = "select * from tableX...";
-
- $result = $SqlDB->Query($sql);
-
- $rs = $SqlDB->getRows($result);
-
- $num = $SqlDB->getRowsNum($result);
-
- ...剩下的操作就是循环取值,
-
- for($i=0;$i<$num;$i++){
- echo($rs[$i]["字段名"]);
- }
-
- ...
最后不要忘记关闭数据路连接哦$SqlDB->Close();当然这句可以不要,php会自动注销!但是这样能够养成一个好的习惯,最好还是加上!其他自己类推...不懂的可以提问!
(责任编辑:admin) |