This article mainly introduces PHP based on the PDO implementation of the powerful MySQL package class, combined with a complete example of PHP based on the implementation of the MySQL database connection, deletion, transaction and other methods, the need for friends to refer to the following
Specific as follows:
Class cpdo{protected $_dsn = "mysql:host=localhost;dbname=test"; protected $_name = "root"; protected $_pass = ""; Protected $_condition = Array (); protected $pdo; protected $fetchAll; protected $query; protected $result; protected $num; protected $mode; protected $prepare; protected $row; protected $fetchAction; protected $beginTransaction; protected $rollback; protected $commit; protected $char; private static $get _mode; private static $get _fetch_action; /** *PDO Construct */Public function __construct ($pconnect = False) {$this->_condition = array (pdo::attr_persistent = $pconnect); $this->pdo_connect (); }/** *pdo Connect */Private Function Pdo_connect () {try{$this->pdo = new PDO ($this->_dsn, $this->_name, $thi S->_pass, $this->_condition); } catch (Exception $e) {return $this->setexceptionerror ($e->getmessage (), $e->getline, $e->getfile); }}/** *self SQL Get Value action */Public function Getvaluebyselfcreatesql ($sql, $fetchAction= "Assoc", $mode = null) {$this->fetchaction = $this->fetchaction ($fetchAction); $this->result = $this->setattribute ($sql, $this->fetchaction, $mode); $this->allvalue = $this->result->fetchall (); return $this->allvalue; }/** *select Condition can query */Private Function SetAttribute ($sql, $fetchAction, $mode) {$this->mode = Self::ge Tmode ($mode); $this->fetchaction = self::fetchaction ($fetchAction); $this->pdo->setattribute (pdo::attr_case, $this->mode); $this->query = $this->base_query ($sql); $this->query->setfetchmode ($this->fetchaction); return $this->query; }/** *get Mode action */private static function GetMode ($get _style) {switch ($get _style) {case null:self:: $get _mo de = pdo::case_natural; Break Case true:self:: $get _mode = Pdo::case_upper; Break Case false; Self:: $get _mode= pdo::case_lower; Break } return Self:: $get _mode; }/** *fetch Value action */private static function fetchAction ($fetchAction) {switch ($fetchAction) {case ' assoc ': Self:: $get _fetch_action = PDO::FETCH_ASSOC;//asso array Break Case ' num ': Self:: $get _fetch_action = pdo::fetch_num; num array break; Case ' object ': Self:: $get _fetch_action = pdo::fetch_obj; Object array break; Case "Both": Self:: $get _fetch_action = Pdo::fetch_both; Assoc array and num array break; Default:self:: $get _fetch_action = PDO::FETCH_ASSOC; Break } return Self:: $get _fetch_action; }/** *get Total num Action */Public Function RowCount ($sql) {$this->result = $this->base_query ($sql); $this->num = $this->result->rowcount (); return $this->num; }/* *simple query and easy query action */Public Function query ($table, $column = "*", $condition = Array (), $group = "", $order = "", $having = "", $startSet = "", $endSet = "", $fetchAction = "Assoc", $params = null) {$sql = "select". $column. " From ' ". $table." ` "; if ($condition! = null) {foreach ($condition as $key = = $value) {$where. = "$key = ' $value ' and"; } $sql. = "where $where"; $sql. = "1 = 1"; if ($group! = "") {$sql. = "GROUP by". $group. " "; if ($order! = "") {$sql. = "ORDER BY". $order. " "; if ($having! = "") {$sql. = "Having ' $having '"; if ($startSet! = "" && $endSet! = "" && is_numeric ($endSet) && is_numeric ($startSet)) {$sql . = "Limit $startSet, $endSet"; } $this->result = $this->getvaluebyselfcreatesql ($sql, $fetchAction, $params); return $this->result; }/** *execute Delete update insert and so on action */Public Function exec ($sql) {$this->result = $this->pdo-> ; exec ($sql); $substr = substr ($sql, 0, 6); if ($this->result) {return $this->successful ($SUBSTR); } else {return $this->fail ($SUBSTR); }}/** *prepare action */Public function prepare ($sql) {$this->prepare = $this->pdo->prepare ($sql); $this->setchars (); $this->prepare->execute (); while ($This->rowz = $this->prepare->fetch ()) {return $this->row; }}/** *use transaction */Public Function Transaction ($sql) {$this->begin (); $this->result = $this->pdo->exec ($sql); if ($this->result) {$this->commit (); } else {$this->rollback (); }}/** *start transaction */Private function begin () {$this->begintransaction = $this->pdo->begintransaction ( ); return $this->begintransaction; }/** *commit Transaction */Private Function commit () {$this->commit = $this->pdo->commit (); return $this->commit; }/** *rollback Transaction */Private Function rollback () {$this->rollback = $this->pdo->rollback (); return $this->rollback; }/** *base Query */Private Function Base_query ($sql) {$this->setchars (); $this->query = $this->pdo->query ($sql); return $this->query; }/** *set chars */Private Function SetChars () {$this->char = $this->pdo->query ("Set NAMES ' UTF8 '"); return $this->char; }/** *process sucessful action */Private function successful ($params) {return "the". $params. "Action is successful"; }/** *process fail action */Private function fail ($params) {return "the". $params. "Action is Fail";}/** *process exc Eption Action */Private function Setexceptionerror ($getMessage, $getLine, $getFile) {echo "Error message is". $getMessa GE. " <br/> The ". $getLine." Line <br/> This file dir on ". $getFile; Exit (); }}
The above is the whole content of this article, I hope that everyone's study has helped.