You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
241 lines
6.1 KiB
241 lines
6.1 KiB
<?php
|
|
class DB_Access {
|
|
private $mysql_address = "";
|
|
private $mysql_username = "";
|
|
private $mysql_password = "";
|
|
private $mysql_database = "";
|
|
|
|
private $dbConn;
|
|
private $last_sql = "";
|
|
private $last_id = 0;
|
|
private $last_num_rows = 0;
|
|
private $error_message = "";
|
|
protected $hasActiveTransaction = false;
|
|
|
|
public function __construct($mysql_address, $mysql_database, $mysql_username, $mysql_password) {
|
|
//設定語系是萬國語言以支援中文
|
|
try {
|
|
// $this->mysql_address = $mysql_address;
|
|
// $this->mysql_database = $mysql_database;
|
|
// $this->mysql_username = $mysql_username;
|
|
// $this->mysql_database = $mysql_database;
|
|
|
|
$dbConn = new PDO("mysql:host=".$mysql_address.";charset=utf8mb4;dbname=".$mysql_database, $mysql_username, $mysql_password);
|
|
$dbConn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);//Suggested to comment on production websites
|
|
$dbConn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
|
|
|
|
$this->dbConn = $dbConn;
|
|
|
|
} catch(PDOException $e) {
|
|
//show error
|
|
echo '<p class="bg-danger">'.$e->getMessage().'</p>';
|
|
|
|
exit;
|
|
}
|
|
}
|
|
|
|
public function __destruct() {
|
|
//切斷跟資料庫的連接
|
|
$this->dbConn = null;
|
|
}
|
|
|
|
/**
|
|
* 這段用來執行 MYSQL 資料庫的語法,可以靈活使用
|
|
*/
|
|
public function execute($sql = null, $data_array): array {
|
|
try {
|
|
$stmt = $this->dbConn->prepare($sql);
|
|
$stmt->execute($data_array);
|
|
return $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
} catch(PDOException $e) {
|
|
$this->error_message = '<p class="bg-danger">'.$e->getMessage().'</p>';
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 這段用來讀取資料庫中的資料,回傳的是陣列資料
|
|
*/
|
|
public function queryDB($table, $condition, $order_by, $fields, $limit, $data_array){
|
|
if(!isset($data_array) OR count($data_array) == 0)return false;
|
|
if(empty($table))return false;
|
|
if(is_numeric($limit))$limit = "LIMIT ".$limit;
|
|
if(empty($condition))$condition = 1;
|
|
if(empty($order_by))$order_by = 1;
|
|
if(empty($fields))$fields = "*";
|
|
$this->last_sql = "SELECT {$fields} FROM {$table} WHERE {$condition} ORDER BY {$order_by} {$limit}";
|
|
try {
|
|
$stmt = $this->dbConn->prepare($this->last_sql);
|
|
$stmt->execute($data_array);
|
|
return $stmt->fetchAll();
|
|
} catch(PDOException $e) {
|
|
$this->error_message = '<p class="bg-danger">'.$e->getMessage().'</p>';
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 這段可以新增資料庫中的資料,並把最後一筆的 ID 存到變數中,可以用 getLastId() 取出
|
|
*/
|
|
public function insert($table = null, $data_array = array()) {
|
|
if($table===null)return false;
|
|
if(count($data_array) == 0) return false;
|
|
|
|
$tmp_col = array();
|
|
$tmp_dat = array();
|
|
|
|
foreach ($data_array as $key => $value) {
|
|
$tmp_col[] = $key;
|
|
$tmp_dat[] = ":$key";
|
|
$prepare_array[":".$key] = $value;
|
|
}
|
|
$columns = join(",", $tmp_col);
|
|
$data = join(",", $tmp_dat);
|
|
|
|
$this->last_sql = "INSERT INTO " . $table . "(" . $columns . ")VALUES(" . $data . ")";
|
|
$stmt = $this->dbConn->prepare($this->last_sql);
|
|
$stmt->execute($prepare_array);
|
|
$this->last_id = $this->dbConn->lastInsertId();
|
|
}
|
|
|
|
/**
|
|
* 這段可以更新資料庫中的資料
|
|
*/
|
|
public function update($table = null, $data_array = null, $key_column = null, $id = null) {
|
|
if($table == null)return false;
|
|
if($id == null) return false;
|
|
if($key_column == null) return false;
|
|
if(count($data_array) == 0) return false;
|
|
|
|
$settingArr = array();
|
|
foreach( $data_array as $key=>$val ){
|
|
$settingArr[]= $key . "=" . ':'.$key;
|
|
}
|
|
$setting_list = implode(",", $settingArr);
|
|
$data_array[$key_column] = $id;
|
|
$this->last_sql = "UPDATE " . $table . " SET " . $setting_list . " WHERE " . $key_column . " = " . ":".$key_column;
|
|
$stmt = $this->dbConn->prepare($this->last_sql);
|
|
$stmt->execute($data_array);
|
|
return true;
|
|
}
|
|
/**
|
|
* 這段可以刪除資料庫中的資料
|
|
*/
|
|
public function delete($table = null, $key_column = null, $id = null) {
|
|
if ($table===null) return false;
|
|
if($id===null) return false;
|
|
if($key_column===null) return false;
|
|
|
|
$this->last_sql = "DELETE FROM $table WHERE " . $key_column . " = " . ':'.$key_column;
|
|
$stmt = $this->dbConn->prepare($this->last_sql);
|
|
$stmt->execute(array( ':'.$key_column => $id));
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* begin transaction
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function transaction(): bool {
|
|
if ( $this->hasActiveTransaction ) {
|
|
return false;
|
|
} else {
|
|
$this->hasActiveTransaction = $this->dbConn->beginTransaction();
|
|
return $this->hasActiveTransaction;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* commit query
|
|
*
|
|
* @return void
|
|
*/
|
|
public function commitDB(){
|
|
$this->dbConn->commit();
|
|
$this->hasActiveTransaction = false;
|
|
}
|
|
|
|
/**
|
|
* rollback query
|
|
*
|
|
* @return void
|
|
*/
|
|
public function rollbackDB(){
|
|
$this->dbConn->rollback();
|
|
$this->hasActiveTransaction = false;
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
* 這段會把最後執行的語法回傳給你
|
|
*/
|
|
public function getLastSql() {
|
|
return $this->last_sql;
|
|
}
|
|
|
|
/**
|
|
* @param string $last_sql
|
|
* 這段是把執行的語法存到變數裡,設定成 private 只有內部可以使用,外部無法呼叫
|
|
*/
|
|
private function setLastSql($last_sql) {
|
|
$this->last_sql = $last_sql;
|
|
}
|
|
|
|
/**
|
|
* @return int
|
|
* 主要功能是把新增的 ID 傳到物件外面
|
|
*/
|
|
public function getLastId() {
|
|
$this->last_id = $this->dbConn->lastInsertId();
|
|
return $this->last_id;
|
|
}
|
|
|
|
/**
|
|
* @param int $last_id
|
|
* 把這個 $last_id 存到物件內的變數
|
|
*/
|
|
private function setLastId($last_id) {
|
|
$this->last_id = $last_id;
|
|
}
|
|
|
|
/**
|
|
* @return int
|
|
*/
|
|
public function getLastNumRows() {
|
|
return $this->last_num_rows;
|
|
}
|
|
|
|
/**
|
|
* close database connection
|
|
*
|
|
* @return void
|
|
*/
|
|
public function closeDB(){
|
|
$this->dbConn = null;
|
|
}
|
|
|
|
/**
|
|
* @param int $last_num_rows
|
|
*/
|
|
private function setLastNumRows($last_num_rows) {
|
|
$this->last_num_rows = $last_num_rows;
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
* 取出物件內的錯誤訊息
|
|
*/
|
|
public function getErrorMessage()
|
|
{
|
|
return $this->error_message;
|
|
}
|
|
|
|
/**
|
|
* @param string $error_message
|
|
* 記下錯誤訊息到物件變數內
|
|
*/
|
|
private function setErrorMessage($error_message)
|
|
{
|
|
$this->error_message = $error_message;
|
|
}
|
|
|
|
}
|