preface

PDO 1 is the abbreviation of PHP data object (PHP Data Object).

can't use PDO extension to perform any database operations. It must use a database-specific PDO driver (for PDO drive of specific database) to access database server.

PDO does not provide database abstraction. It does not rewrite SQL or provide functions that are missing from the database itself. If you need this function, you need to use a more sophisticated abstraction layer.

uses PDO MySQL database operation recently in a project, then write a class file named mysql_class.php code as follows:

 < sample code; PHP? Class mysql{const DSN / / declare = "mysql:host=[address database dbname=[database name]]; charset=utf8;"; / / database and database name and address encoding const USERNAME = [database username] "; / / PASSWD =" const username [password] database "; / / code / / private $sql private declarations = NULL; //sql = NULL $link private statement cache; / / $result database connection private = NULL; / / results /******************************************************************************* * @ Name: Connect * @ attribute Private function connect (private *******************************************************************************/) {try {$this-> link = new PDO (self:: DSN, self:: USERNAME, self:: PASSWD, array (PDO:: ATTR_ERRMODE => PDO:: ERRMODE_EXCEPTION)); / / create a connection (PDOException $e) {catch} die ("Error!" (.$e-> getMessage "). < hr/>");}} /******************************************************************************* * @ * @ Name: execute SQL statement: private property * @ * @ entrance parameters: no export parameters: successful execution returns true, otherwise returns false, the query result set storage array *******************************************************************************/ private function (SQL) {try {$this-> Co (nnect); $this-> link-> (beginTransaction); / / start a transaction $prepare = $this-> link-> prepare ($this-> SQL); / / prepared statements ($prepare-> execute); / / execute the query and return the result set $cmd = strtolower (substr (trim ($this-> SQL), 0,6)) interception; / / command character if ($cmd = = select) {$array = $prepare-> fetch (PDO:: FETCH_ASSOC); / / get the all data if concentration (count ($array)) {$this-> result = NULL; $this- > result = $array; / / true / / return storage result set to query results;}else{return false / / returns true; otherwise returns false}}else if ($cmd = = "insert" $cmd "delete" || || = = $cmd = = update) {if ($prepare) {return true; / / return}else{retur really succeeds N false; otherwise returns false}} / / $this-> link-> (COMMIT); / / if the correct execution then confirm the commit (PDOException $e)} catch {$this-> link-> (rollBack); / / if there is an error in case a rollback in the execution of the die ("Error!" (.$e-> getMessage). "< hr/>");}} /******************************************************************************* * @ * @ Name: SQL statement processing attributes: public * @ entrance parameters: CMD crud character command; dsname data; the first parameter of first second; second parameters; * @ export parameters: successful execution returns true, otherwise returns false query the operation returns a result set * @ array use example: $mysql-> handle ("insert", "ABC", "openid, nickname", "'123','abc'"); / / $mysql-> Handle ("delete", "ABC", "openid='123'"); / / delete $mysql-> handle ("update", "ABC", "nickname='def'", "openid='123'"); / / update $res = $mysql-> handle ("select", "ABC", "*", "openid='123'"); / / query if (is_array ($res) = = true) {foreach ($res as $key=> $val) {echo $key. "=".$val. "< hr>}";}// traversal query results *******************************************************************************/ public function handle array ($cmd, $dsname, $first, $second=NULL) {switch ($cmd) case'insert': $this-> {/ / insert; SQL = "insert into $dsname ($first) values ($second); break; case:'delete' / / delete $this-> SQL =" delete from $dsname where $first; break; case "'update': / / update $this-> SQL =" update $dsname Set $first where $second "; break case;'select': / / $this-> query; SQL =" select $first from $dsname where $second "; break; default: die (" Syntax Error! "); / / $res prompt syntax error break = $this->}; (SQL); / / execute SQL statements if ($res) {if ($cmd = ='select') {return $this-> result}else{return; / / return query results true; / /}else{return successfully executed returns true}} / / false; otherwise returns false $this-> link=NULL; $mysql;}} / / close database = new MySQL; / / instantiate database class > 

above are all the content of this article, I hope this paper has some reference to learn to learn or work Value, if there is a doubt you can leave a message and exchange, thank you for the support of the home of the script.

This paper fixed link:http://www.script-home.com/self-written-use-of-pdo-to-delete-and-delete-operation-classes-of-mysql-database.html | Script Home | +Copy Link

Article reprint please specify:Self written use of PDO to delete and delete operation classes of MySQL database | Script Home

You may also be interested in these articles!