PHP数据库处理封装类实例

前端之家收集整理的这篇文章主要介绍了PHP数据库处理封装类实例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

MysqL的操作相关类,检查并使用了MysqLi

PHP;"> 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: @H_404_5@"; $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: @H_404_5@"; $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: @H_404_5@"; $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: @H_404_5@"; $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: @H_404_5@"; $exceptionstring .= $this->db->errno . ": " . $this->db->error; throw new exception ($exceptionstring); } else { echo "Query performed correctly: " . $this->db->affected_rows . " row(s) affected.@H_404_5@"; } //MysqL functionality. } else { if (!MysqL_query ($thequery,$this->db)){ $exceptionstring = "Error performing query: $thequery: @H_404_5@"; $exceptionstring .= MysqL_errno() . ": " . MysqL_error(); throw new exception ($exceptionstring); } else { echo "Query performed correctly: " . MysqL_affected_rows () . " row(s) affected.@H_404_5@"; } } } 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: @H_404_5@"; $exceptionstring .= $this->db->errno . ": " . $this->db->error; throw new exception ($exceptionstring); } //MysqL functionality. } else { if (!$aquery = MysqL_query ($thequery)){ $exceptionstring = "Error performing query: $thequery: @H_404_5@"; $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: @H_404_5@"; $exceptionstring .= $this->db->errno . ": " . $this->db->error; throw new exception ($exceptionstring); } //MysqL functionality. } else { if (!MysqL_close ($this->db)){ $exceptionstring = "Error closing connection: @H_404_5@"; $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] . "@H_404_5@"; } $mydb->selectdb("test"); $result = $mydb->execute("UPDATE user SET age = 23 WHERE id = 2"); echo "@H_404_5@"; echo $result; ?>

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

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

原文链接:https://www.f2er.com/php/18209.html

猜你在找的PHP相关文章