我來管理我所有的數據庫查詢一個PHP數據庫類:我是否需要爲每個查詢實例化Php數據庫類?
class DatabaseConnection()
{
private $link;
public $filter;
public function log_db_errors($error, $query)
{
if(MY_DEBUG)
{
echo $message;
}
}
public function __construct()
{
global $connection;
mb_internal_encoding('UTF-8');
mb_regex_encoding('UTF-8');
$this->link = new mysqli(MY_HOST, MY_USER, MY_PASS, MY_DB);
$this->link->set_charset("utf8");
if($this->link->connect_errno)
{
$this->log_db_errors("Connect failed", $this->link->connect_error);
echo 'Server error. Please try again sometime. DB';
exit();
}
}
public function __destruct()
{
$this->disconnect();
}
public function filter($data)
{
if(!is_array($data))
{
$data = trim(htmlentities($data));
$data = $this->link->real_escape_string($data);
}
else
{
$data = array_map(array('DB', 'filter'), $data);
}
return $data;
}
public function query($query)
{
$full_query = $this->link->query($query);
if($this->link->error)
{
$this->log_db_errors($this->link->error, $query);
$full_query->free();
return false;
}
else
{
$full_query->free();
return true;
}
}
public function my_table_exists_create($table, $variables = array()) {
$check = $this->link->query("SELECT * FROM '$table' LIMIT 1");
if($check) return true;
else {
if(empty($variables)) {
return false;
exit;
}
$sql = "CREATE TABLE IF NOT EXISTS ". $table;
$fields = array();
$values = array();
foreach($variables as $field) {
$fields[] = $field; //$values[] = "'".$value."'";
}
$fields = ' (' . implode(', ', $fields) . ')';
$sql .= $fields;
$query = $this->link->query($sql);
if($this->link->error) {
$this->log_db_errors($this->link->error, $sql);
return false;
}
else return true;
}
}
public function my_num_rows($query)
{
$num_rows = $this->link->query($query);
if($this->link->error)
{
$this->log_db_errors($this->link->error, $query);
return $this->link->error;
}
else
{
return $num_rows->num_rows;
}
}
public function exists($table = '', $check_val = '', $params = array())
{
if(empty($table) || empty($check_val) || empty($params))
{
return false;
exit;
}
$check = array();
foreach($params as $field => $value)
{
if(!empty($field) && !empty($value))
{
if($this->db_common($value))
{
$check[] = "$field = $value";
}
else
{
$check[] = "$field = '$value'";
}
}
}
$check = implode(' AND ', $check);
$rs_check = "SELECT $check_val FROM ".$table." WHERE $check";
$number = $this->my_num_rows($rs_check);
if($number === 0)
{
return false;
}
else
{
return true;
}
exit;
}
public function disconnect()
{
$this->link->close();
}
}
我使用這個類來管理我所有的問題,就像插入到數據庫:
$database = new DatabaseConnection();
$Item_Details = array(
'item_title' => trim($_POST['title']),
'item_content' => trim($_POST['content']),
'item_updated' => date('Y-m-d H:i:s'),
'item_updatedby' => $my_loginid,
);
$where_clause = array('itemid' => $itemid);
$updated = $database->as_update('my_item', $Item_Details , $where_clause, 1);
現在我需要知道我可以使用這個類,而沒有太多的連接可能會減慢連接到服務器的連接,從而導致超時和連接太多。我以爲我可以使用一個全局變量
function my_constant_initialize()
{
global $databasecon;
$databasecon = new DatabaseConnection();
return $databasecon;
}
所以請建議如何避免太多的連接。此外,以及告訴是否有必要爲每個查詢實例化數據庫類,或者我可以只調用一次,因爲我在我的php代碼中使用了大量的Include和require函數。
不知道我明白,但爲了使用你的類的單個實例,你可以使用單例模式。 – Andreas
安德烈亞斯請你指教我單身人士 –
單身模式是你的班級首先檢查一個實例是否已經存在,如果它確實返回了這個實例而不是新建一個實例。其實很簡單,只需在網絡上搜索實施。 – isa424