加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > PHP > 正文

PHP数据库处理封装类实例

发布时间:2020-05-23 04:50:40 所属栏目:PHP 来源:互联网
导读:这篇文章主要介绍了PHP数据库处理封装类,结合完整实例形式分析了php基于mysqli封装的数据库连接及增删改查等操作,需要的朋友可以参考下

本文实例讲述了PHP数据库处理封装类。分享给大家供大家参考,具体如下:

MySQL的操作相关类,检查并使用了mysqli

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:
"; $exceptionstring .= mysqli_connect_errno() . ": " . mysqli_connect_error(); throw new exception ($exceptionstring); } //Mysql functionality. } else { if (!$this->db = mysql_connect ($this->host,$this->pass)){ $exceptionstring = "Error connection to database:
"; $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:
"; $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:
"; $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:
"; $exceptionstring .= $this->db->errno . ": " . $this->db->error; throw new exception ($exceptionstring); } else { echo "Query performed correctly: " . $this->db->affected_rows . " row(s) affected.
"; } //Mysql functionality. } else { if (!mysql_query ($thequery,$this->db)){ $exceptionstring = "Error performing query: $thequery:
"; $exceptionstring .= mysql_errno() . ": " . mysql_error(); throw new exception ($exceptionstring); } else { echo "Query performed correctly: " . mysql_affected_rows () . " row(s) affected.
"; } } } 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:
"; $exceptionstring .= $this->db->errno . ": " . $this->db->error; throw new exception ($exceptionstring); } //Mysql functionality. } else { if (!$aquery = mysql_query ($thequery)){ $exceptionstring = "Error performing query: $thequery:
"; $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:
"; $exceptionstring .= $this->db->errno . ": " . $this->db->error; throw new exception ($exceptionstring); } //Mysql functionality. } else { if (!mysql_close ($this->db)){ $exceptionstring = "Error closing connection:
"; $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] . "
"; } $mydb->selectdb("test"); $result = $mydb->execute("UPDATE user SET age = 23 WHERE id = 2"); echo "
"; echo $result; ?>

更多关于PHP相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》、《》、《》及《》

希望本文所述对大家PHP程序设计有所帮助。

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读