PHP數(shù)據(jù)庫(kù)處理封裝類(lèi)實(shí)例
本文實(shí)例講述了PHP數(shù)據(jù)庫(kù)處理封裝類(lèi)。分享給大家供大家參考,具體如下:
MySQL的操作相關(guān)類(lèi),檢查并使用了mysqli
<?php
//sample15_12.php
class mydb {
private $user;
private $pass;
private $host;
private $db;
//Constructor function.
public function __construct (){
$num_args = func_num_args();
if($num_args > 0){
$args = func_get_args();
$this->host = $args[0];
$this->user = $args[1];
$this->pass = $args[2];
$this->connect();
}
}
//Function to tell us if mysqli is installed.
private function mysqliinstalled (){
if (function_exists ("mysqli_connect")){
return true;
} else {
return false;
}
}
//Function to connect to the database.
private function connect (){
try {
//Mysqli functionality.
if ($this->mysqliinstalled()){
if (!$this->db = new mysqli ($this->host,$this->user,$this->pass)){
$exceptionstring = "Error connection to database: <br />";
$exceptionstring .= mysqli_connect_errno() . ": " . mysqli_connect_error();
throw new exception ($exceptionstring);
}
//Mysql functionality.
} else {
if (!$this->db = mysql_connect ($this->host,$this->user,$this->pass)){
$exceptionstring = "Error connection to database: <br />";
$exceptionstring .= mysql_errno() . ": " . mysql_error();
throw new exception ($exceptionstring);
}
}
} catch (exception $e) {
echo $e->getmessage();
}
}
//Function to select a database.
public function selectdb ($thedb){
try {
//Mysqli functionality.
if ($this->mysqliinstalled()){
if (!$this->db->select_db ($thedb)){
$exceptionstring = "Error opening database: $thedb: <br />";
$exceptionstring .= $this->db->errno . ": " . $this->db->error;
throw new exception ($exceptionstring);
}
//Mysql functionality.
} else {
if (!mysql_select_db ($thedb, $this->db)){
$exceptionstring = "Error opening database: $thedb: <br />";
$exceptionstring .= mysql_errno() . ": " . mysql_error();
throw new exception ($exceptionstring);
}
}
} catch (exception $e) {
echo $e->getmessage();
}
}
//Function to perform a query.
public function execute ($thequery){
try {
//Mysqli functionality.
if ($this->mysqliinstalled()){
if (!$this->db->query ($thequery)){
$exceptionstring = "Error performing query: $thequery: <br />";
$exceptionstring .= $this->db->errno . ": " . $this->db->error;
throw new exception ($exceptionstring);
} else {
echo "Query performed correctly: " . $this->db->affected_rows . " row(s) affected.<br />";
}
//Mysql functionality.
} else {
if (!mysql_query ($thequery, $this->db)){
$exceptionstring = "Error performing query: $thequery: <br />";
$exceptionstring .= mysql_errno() . ": " . mysql_error();
throw new exception ($exceptionstring);
} else {
echo "Query performed correctly: " . mysql_affected_rows () . " row(s) affected.<br />";
}
}
} catch (exception $e) {
echo $e->getmessage();
}
}
//Function to return a row set.
public function getrows ($thequery){
try {
//Mysqli functionality.
if ($this->mysqliinstalled()){
if ($result = $this->db->query ($thequery)){
$returnarr = array ();
while ($adata = $result->fetch_array ()){
$returnarr = array_merge ($returnarr,$adata);
}
return $returnarr;
} else {
$exceptionstring = "Error performing query: $thequery: <br />";
$exceptionstring .= $this->db->errno . ": " . $this->db->error;
throw new exception ($exceptionstring);
}
//Mysql functionality.
} else {
if (!$aquery = mysql_query ($thequery)){
$exceptionstring = "Error performing query: $thequery: <br />";
$exceptionstring .= mysql_errno() . ": " . mysql_error();
throw new exception ($exceptionstring);
} else {
$returnarr = array ();
while ($adata = mysql_fetch_array ($aquery)){
$returnarr = array_merge ($returnarr,$adata);
}
return $returnarr;
}
}
} catch (exception $e) {
echo $e->getmessage();
}
}
//Function to close the database link.
public function __destruct() {
try {
//Mysqli functionality.
if ($this->mysqliinstalled()){
if (!$this->db->close()){
$exceptionstring = "Error closing connection: <br />";
$exceptionstring .= $this->db->errno . ": " . $this->db->error;
throw new exception ($exceptionstring);
}
//Mysql functionality.
} else {
if (!mysql_close ($this->db)){
$exceptionstring = "Error closing connection: <br />";
$exceptionstring .= mysql_errno() . ": " . mysql_error();
throw new exception ($exceptionstring);
}
}
} catch (exception $e) {
echo $e->getmessage();
}
}
}
//Now, let us create an instance of mydb.
$mydb = new mydb ("localhost","root","");
//Select a database to use.
$mydb->selectdb ("wps");
//Now, let's perform an action.
//$adata = $mydb->execute ("UPDATE cd SET title='Hybrid Theory' WHERE cdid='2'");
//Then, let's try to return a row set.
$adata = $mydb->getrows ("SELECT * FROM wp_terms");
for ($i = 0; $i < count ($adata); $i++){
echo $adata[$i] . "<br />";
}
$mydb->selectdb("test");
$result = $mydb->execute("UPDATE user SET age = 23 WHERE id = 2");
echo "<br />";
echo $result;
?>
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》、《PHP基于pdo操作數(shù)據(jù)庫(kù)技巧總結(jié)》、《PHP+MongoDB數(shù)據(jù)庫(kù)操作技巧大全》、《php+Oracle數(shù)據(jù)庫(kù)程序設(shè)計(jì)技巧總結(jié)》、《php+mssql數(shù)據(jù)庫(kù)程序設(shè)計(jì)技巧總結(jié)》、《php+redis數(shù)據(jù)庫(kù)程序設(shè)計(jì)技巧總結(jié)》、《php+mysqli數(shù)據(jù)庫(kù)程序設(shè)計(jì)技巧總結(jié)》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- php封裝db類(lèi)連接sqlite3數(shù)據(jù)庫(kù)的方法實(shí)例
- php db類(lèi)庫(kù)進(jìn)行數(shù)據(jù)庫(kù)操作
- PHP基于MySQLI函數(shù)封裝的數(shù)據(jù)庫(kù)連接工具類(lèi)【定義與用法】
- PHP數(shù)據(jù)庫(kù)表操作的封裝類(lèi)及用法實(shí)例詳解
- PHP封裝的PDO數(shù)據(jù)庫(kù)操作類(lèi)實(shí)例
- php簡(jiǎn)單數(shù)據(jù)庫(kù)操作類(lèi)的封裝
- PHP封裝mysqli基于面向?qū)ο蟮膍ysql數(shù)據(jù)庫(kù)操作類(lèi)與用法示例
- PHP封裝的mysqli數(shù)據(jù)庫(kù)操作類(lèi)示例
- PHP模型Model類(lèi)封裝數(shù)據(jù)庫(kù)操作示例
- PHP封裝的數(shù)據(jù)庫(kù)模型Model類(lèi)完整示例【基于PDO】
- PHP封裝類(lèi)似thinkphp連貫操作數(shù)據(jù)庫(kù)Db類(lèi)與簡(jiǎn)單應(yīng)用示例
相關(guān)文章
php在服務(wù)器執(zhí)行exec命令失敗的解決方法
出于安全的原因,服務(wù)器是不允許php或者其他語(yǔ)言執(zhí)行exec命令的,當(dāng)你有特殊需要php在服務(wù)器執(zhí)行exec命令時(shí),你需要設(shè)置兩個(gè)地方,不然就無(wú)法執(zhí)行成功2012-03-03
微信公眾平臺(tái)接口開(kāi)發(fā)入門(mén)示例
這篇文章主要介紹了微信公眾平臺(tái)接口開(kāi)發(fā)入門(mén)示例,較為簡(jiǎn)單透徹的分析了微信公眾平臺(tái)接口開(kāi)發(fā)的技巧與具體方法,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2014-12-12
談?wù)凱HP連接Access數(shù)據(jù)庫(kù)的注意事項(xiàng)
有的時(shí)候需要用php連接access數(shù)據(jù)庫(kù),結(jié)果整了半天Access數(shù)據(jù)庫(kù)就是連接不上,查找很多資料,以下是些個(gè)人經(jīng)驗(yàn),希望能給需要連接access 數(shù)據(jù)的人帶來(lái)幫助。2016-08-08
php實(shí)現(xiàn)監(jiān)控varnish緩存服務(wù)器的狀態(tài)
這篇文章主要介紹了php實(shí)現(xiàn)監(jiān)控varnish緩存服務(wù)器的狀態(tài),Varnish是一款高性能的開(kāi)源HTTP加速器,可以替代Squid、Nginx等服務(wù)器,需要的朋友可以參考下2014-12-12
php將金額數(shù)字轉(zhuǎn)化為中文大寫(xiě)
本文給大家匯總介紹了幾種php將金額數(shù)字轉(zhuǎn)化為中文大寫(xiě)的實(shí)用函數(shù),各有優(yōu)劣,小伙伴們根據(jù)自己的項(xiàng)目需求自由選擇吧。2015-07-07

