PHP 資料庫操作類

2022-03-02 15:18:33 字數 3084 閱讀 5001

---db.php---

<?php

class

db }

//資料庫連線

public

function connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect = 0,$charset='utf8')

} else

}if(!@mysql_select_db($dbname,$this->link_id))

@mysql_query("set names ".$charset

); }

//查詢

public

function query($sql

)

//獲取一條記錄(mysql_assoc,mysql_num,mysql_both)

public

function get_one($sql,$result_type =mysql_assoc)

//獲取全部記錄

public

function get_all($sql,$result_type =mysql_assoc)

$this->write_log("獲取全部記錄 ".$sql

);

return

$rt;

}//插入

public

function insert($table,$dataarray

)

while(list($key,$val)=each($dataarray

))

$field = substr( $field,0,-1);

$value = substr( $value,0,-1);

$sql = "insert into $table($field) values($value)";

$this->write_log("插入 ".$sql

);

if(!$this->query($sql)) return

false

;

return

true

; }

//更新

public

function update( $table,$dataarray,$condition="")

$value = "";

while( list($key,$val) = each($dataarray

))

$value .= "$key = '$val',";

$value .= substr( $value,0,-1);

$sql = "update $table set $value where 1=1 and $condition";

$this->write_log("更新 ".$sql

);

if(!$this->query($sql)) return

false

;

return

true

; }

//刪除

public

function delete( $table,$condition="")

$sql = "delete from $table where 1=1 and $condition";

$this->write_log("刪除 ".$sql

);

if(!$this->query($sql)) return

false

;

return

true

; }

//返回結果集

public

function fetch_array($query, $result_type =mysql_assoc)

//獲取記錄條數

public

function num_rows($results

) else

}//釋放結果集

public

function

free_result()

}$this->write_log("釋放結果集");

}//獲取最後插入的id

public

function

insert_id()

//關閉資料庫連線

protected

function

close()

//錯誤提示

private

function halt($msg='')

//析構函式

public

function

__destruct()

}//寫入日誌檔案

public

function write_log($msg='')

}//獲取毫秒數

public

function

microtime_float() }

?>

---config.db.php---

<?php 

$db_config["hostname"] = "localhost"; //

伺服器位址

$db_config["username"] = "root"; //

資料庫使用者名稱

$db_config["password"] = "123"; //

資料庫密碼

$db_config["database"] = "test"; //

資料庫名稱

$db_config["charset"] = "utf8";//

資料庫編碼

$db_config["pconnect"] = 1;//

開啟持久連線

$db_config["log"] = 1;//

開啟日誌

$db_config["logfilepath"] = './';//

開啟日誌

?>

php 資料庫操作類

class dbconnect if dbtable this dbchange dbtable 選擇要操作的資料庫 function dbchange dbtable type 0 返回執行完sql語句後的結果 不做處理 type 1 返回 看下面注釋 type 2 返回 看下面注釋 functi...

php資料庫操作類

config.db.php db config hostname localhost 伺服器位址 db config username root 資料庫使用者名稱 db config password 123 資料庫密碼 db config database test 資料庫名稱 db config...

PHP資料庫操作類

class db 資料庫連線 public function connect dbhost,dbuser,dbpw,dbname,pconnect 0,charset utf8 else if mysql select db dbname,this link id mysql query set n...