您好,登錄后才能下訂單哦!
本文小編為大家詳細(xì)介紹“php如何封裝數(shù)據(jù)庫(kù)增刪改的類(lèi)”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“php如何封裝數(shù)據(jù)庫(kù)增刪改的類(lèi)”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來(lái)學(xué)習(xí)新知識(shí)吧。
1.為什么需要封裝數(shù)據(jù)庫(kù)類(lèi)?
通常情況下,在進(jìn)行數(shù)據(jù)庫(kù)操作時(shí),我們會(huì)在應(yīng)用程序的主要邏輯中進(jìn)行操作,比如在控制器中操作數(shù)據(jù)庫(kù)。但實(shí)際上,這種方式存在幾個(gè)明顯的問(wèn)題:
代碼冗余:在我們的應(yīng)用程序中,很可能多個(gè)控制器需要對(duì)數(shù)據(jù)庫(kù)進(jìn)行操作。如果每個(gè)控制器都單獨(dú)編寫(xiě)數(shù)據(jù)庫(kù)操作的代碼,那么就會(huì)出現(xiàn)大量的冗余代碼。
代碼可讀性差:這種情況下,代碼的可讀性也就差了。很可能我們同一段代碼就出現(xiàn)在多個(gè)控制器中,這樣一來(lái),就會(huì)給其他開(kāi)發(fā)人員帶來(lái)困擾。
安全問(wèn)題:將數(shù)據(jù)庫(kù)操作代碼直接寫(xiě)在應(yīng)用程序中,存在一定的安全風(fēng)險(xiǎn)。比如sql注入等問(wèn)題。
綜上所述,我們有必要將我們的數(shù)據(jù)庫(kù)操作代碼封裝起來(lái),讓它們成為一個(gè)獨(dú)立的模塊。這個(gè)模塊需要具有以下幾個(gè)特點(diǎn):
具有良好的可讀性
能夠方便的進(jìn)行代碼復(fù)用
具有良好的擴(kuò)展性
能夠有效的解決安全問(wèn)題
2.封裝數(shù)據(jù)庫(kù)增刪改類(lèi)的基本思路
針對(duì)上面提到的問(wèn)題,我們可以把類(lèi)寫(xiě)成php庫(kù)的形式,以便實(shí)現(xiàn)類(lèi)的復(fù)用。封裝數(shù)據(jù)庫(kù)增刪改類(lèi)的基本思路如下:
將所有的數(shù)據(jù)庫(kù)操作都封裝在一個(gè)類(lèi)中,比如叫做db類(lèi)。
編寫(xiě)通用的方法來(lái)實(shí)現(xiàn)數(shù)據(jù)庫(kù)增刪改查操作。
在方法中實(shí)現(xiàn)參數(shù)化查詢(xún),從而有效的避免sql注入等安全問(wèn)題。
將所有的錯(cuò)誤信息都捕獲,并且封裝成一個(gè)統(tǒng)一的異常拋出給上層代碼。
下面是一個(gè)簡(jiǎn)單的db類(lèi)的實(shí)現(xiàn):
class db {
private $db_host;
private $db_user;
private $db_pass;
private $db_name;
private $conn;
function __construct($db_host, $db_user, $db_pass, $db_name) {
$this->db_host = $db_host;
$this->db_user = $db_user;
$this->db_pass = $db_pass;
$this->db_name = $db_name;
}
// 數(shù)據(jù)庫(kù)連接方法
function connect() {
$this->conn = mysqli_connect($this->db_host, $this->db_user, $this->db_pass, $this->db_name);
if (!$this->conn) {
throw new Exception('數(shù)據(jù)庫(kù)連接失敗');
}
mysqli_query($this->conn, "SET NAMES 'utf8'");
}
// 數(shù)據(jù)庫(kù)關(guān)閉方法
function close() {
mysqli_close($this->conn);
}
// 查詢(xún)方法,參數(shù)化查詢(xún)
function query($sql, $params) {
$stmt = mysqli_prepare($this->conn, $sql);
if (!$stmt) {
throw new Exception('query error: '.mysqli_error($this->conn));
}
if (count($params) > 0) {
call_user_func_array('mysqli_stmt_bind_param', array_merge(array($stmt, str_repeat('s', count($params))), $params));
}
$result = mysqli_stmt_execute($stmt);
if (!$result) {
throw new Exception('query error: '.mysqli_stmt_error($stmt));
}
$rows = array();
$meta = mysqli_stmt_result_metadata($stmt);
if ($meta) {
while ($field = mysqli_fetch_field($meta)) {
$params[] = &$row[$field->name];
}
call_user_func_array(array($stmt, 'bind_result'), $params);
while (mysqli_stmt_fetch($stmt)) {
$rows[] = $row;
}
}
mysqli_stmt_close($stmt);
return $rows;
}
// 增加方法,參數(shù)化查詢(xún)
function insert($table, $data) {
$sql = 'INSERT INTO '.$table.' ('.implode(',', array_keys($data)).') VALUES ('.implode(',', array_fill(0, count($data), '?')).')';
$result = $this->query($sql, array_values($data));
return mysqli_affected_rows($this->conn);
}
// 更新方法
function update() {
// 實(shí)現(xiàn)更新方法
}
// 刪除方法
function delete() {
// 實(shí)現(xiàn)刪除方法
}
}
3.使用封裝好的類(lèi)
當(dāng)我們需要對(duì)數(shù)據(jù)庫(kù)進(jìn)行增刪改查時(shí),只需要引入db類(lèi),然后實(shí)例化即可。比如:
require_once 'db.php';
$db = new db('localhost', 'root', 'root', 'test');
$db->connect();
$sql = 'INSERT INTO `user`(`name`, `age`) VALUES (?, ?)';
$data = array('Tom', '18');
$db->query($sql, $data);
$db->close();
讀到這里,這篇“php如何封裝數(shù)據(jù)庫(kù)增刪改的類(lèi)”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識(shí)點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過(guò)才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。