PHP Database Processing Encapsulation class

Source: Internet
Author: User
This article mainly introduces the PHP database processing package class, combined with a complete example of PHP based on the MYSQLI package database connection and deletion and modification of the operation, the need for friends can refer to the next

Specific as follows:

MySQL Operation related classes, check and use the 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 the 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)) {$exception            string = "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/&gt            ;"; $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-&GT;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 = Arra            Y ();            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: $thequer            Y: <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 Co            Nnection: <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 a 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 = WHERE id = 2 ");  echo "<br/>"; echo $result;? >

The above is the whole content of this article, I hope that everyone's study has helped.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.