mysql資料庫操作類

2021-06-01 18:51:35 字數 2628 閱讀 7120

mysql資料庫操作類。

配置檔案:

<?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"] = './';//開啟日誌

?>

mysql操作類:

<?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()

}?>

MySql資料庫操作類

getfield sql 獲取某一字段值 db getrow sql 獲取某一行資料 db getlist sql 獲取多行資料 db exe sql 執行資料庫操作 insert update delete.class mysqldb public static function getdb ho...

mysql 操作類 檔案 mysql資料庫操作類

class db else else return 0 釋放結果集 public function free result void func get args foreach void as query if is resource query get resource type query my...

PHP操作MYSQL資料庫類

phprequire once sys info.php class classdatabase else if if function construct 析構函式,關閉資料庫 function destruct function destruct 執行資料庫的更新,插入和刪除 function ...