-
-
class DataBase
- {
- var $pConnect=FALSE;//是否使用長連接
- var $mHost;//資料庫主機
- var $mDatabase;
- var $db; //資料庫
- var $mUser;//資料庫使用者名稱
- var $mPwd;//資料庫使用者密碼
- var $mConn;//連線標識
- var $result;// 執行query指令的結果資源識別
- var $num_rows;// 傳回的條目數
- var $insert_id;// 傳回最後一次使用INSERT 指令的ID
- var $affected_rows;// 傳回query指令所影響的列數目
- // INSERT、UPDATE 或DELETE 所影響的欄位(row) 數目。
- // delete 如果不帶where,那麼則回傳0
- //建構子
- public function __construct($host,$user,$pwd,$db)
- {
- $this ->mHost=$host;
- $this->mUser=$user;
- $this->mPwd=$pwd;
- $this->db=$db;
- }
-
- //資料庫連線
- public function connect()
- {
- if($this->pConnect)
- $this->mConn=mysql_pconnect($this->mHost,$this- >mUser,$this->mPwd);//長連線
- else
- $this->mConn=mysql_connect($this->mHost,$this->mUser,$this->mPwd);// short connect
if(!$this->mConn) $this->dbhalt("無法連接資料庫!");
- if($this->db=="") $this->db=$this->dbDatabase;
-
- if(!mysql_select_db($this->db,$this->mConn))
- $this->dbhalt("資料庫不可用! ");
- } // eof#dbconnect()
//更改資料庫
- public function dbChange($db){
- $this->db=$db ;
- $this->connect();
- }
//執行SQL語句,傳回結果資源id
- public function execute($sql){
- $this->result=mysql_query($sql);
- return $this->result;
- }
//取得陣列-索引與關聯
- public function fetchArray ($resultType=MYSQL_BOTH)
- {
- return mysql_fetch_array($this->result,$resultType);
- }
-
- //取得關聯陣列
- public function fetchAss(unction) 🎜> {
- return mysql_fetch_assoc($this->result);
- }
-
- //取得數字索引陣列
- public function fetchIndexArray()
- {result);
- }
-
- //取得物件陣列
- public function fetchObject()
- {
- return mysql_fetch_object($this->result);
- return mysql_fetch_object($this->result);
-
- }
-
- //回傳記錄行數
- function numRows()
- {
- return mysql_num_rows($this->result);
- }
//回傳主機中所有資料庫名稱
- public function dbNames()
- {
- $rsPtr=mysql_list_dbs($this->mConn);
- $i=0;
- $cnt= mysql_num_rows($rsPtr);
- while($i {
- $rs[]=mysql_db_name($rsPtr,$i);
- $i++;
- }
- }
- return $rs;
- }
function dbhalt($errmsg){
- $msg="資料庫有問題!";
- $msg=$errmsg;
- echo"$msg";
- die();
- }
//刪除
- function delete($sql){
- $result=$this- >execute($sql,$dbbase);
- $this->affected_rows=mysql_affected_rows($this->dbLink);
- $this->free_result($result);
- return $this->affected_rows>affected_rows>affected_rows>affected_rows ;
- }
//增加
- function insert($sql){
- $result=$this->execute($sql,$dbbase);
- $this->insert_id=mysql_insert_id($this->dbLink);
- $this->free_result($result);
- return $this->insert_id;
- }
- //改
- function update($sql){
- $result=$this->execute($sql,$dbbase);
- $this->affected_rows=mysql_affected_rows($this->dbLink );
- $this->free_result($result);
- return $this->affected_rows;
- }
-
- //關閉連線
- function dbclose(){
- mysql_close($this->dbLink);
- }
- }// end class
- ?>
複製代碼
呼叫範例:
-
-
include "class_database.php";
$mydb= new DataBase("localhost","root","123456","test");
- $mydb->connect();
- $mydb->execute("set names GBK");
- $mydb->execute("select * from usrs");
- print_r($mydb->dbNames());
- ?>
-
複製代碼
|
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31