Definition and usage of PDO public class in PHP

Source: Internet
Author: User
Tags dsn
This article mainly introduced the PHP implementation of the PDO public class definition and usage, combined with specific examples of the implementation of the PHP PDO operation class definition and query, insert and other use skills, the need for friends can refer to the next

This paper describes the definition and usage of PDO common class implemented by PHP. Share to everyone for your reference, as follows:

db.class.php:

<?phpclass DB extends \pdo {private static $_instance = null;  protected $dbName = ";  protected $dsn;  protected $DBH; Public function __construct ($dbHost, $dbUser, $dbPasswd, $dbName, $dbCharset = ' utf8 ') {try {$this->dsn = ' mysq L:host= '. $dbHost. ';d bname= '.      $dbName;      $this-&GT;DBH = new \pdo ($this->dsn, $dbUser, $DBPASSWD);      $this->dbh->setattribute (\pdo::attr_emulate_prepares, false);      $this->dbh->setattribute (\pdo::attr_errmode, \pdo::errmode_exception); $this->dbh->exec (' SET character_set_connection= '. $dbCharset. '; SET character_set_client= '. $dbCharset. ';    SET character_set_results= '. $dbCharset);     } catch (Exception $e) {$this->outputerror ($e->getmessage ()); }} public static function getinstance ($dbHost, $dbUser, $dbPasswd, $dbName, $dbCharset = ' utf8 ') {if (Self::$_instanc    E = = null) {self::$_instance = new self ($dbHost, $dbUser, $dbPasswd, $dbName, $dbCharset); } return self::$_instance;  Public Function Fetchall ($sql, $params = Array ()) {try {$stm = $this->dbh->prepare ($sql);      if ($stm && $stm->execute ($params)) {return $stm->fetchall (\PDO::FETCH_ASSOC);    }} catch (Exception $e) {$this->outputerror ($e->getmessage ());      }} Public Function Fetchone ($sql, $params = Array ()) {try {$result = false;      $stm = $this->dbh->prepare ($sql);      if ($stm && $stm->execute ($params)) {$result = $stm->fetch (\PDO::FETCH_ASSOC);    } return $result;    } catch (Exception $e) {$this->outputerror ($e->getmessage ());    }} Public Function Fetchcolumn ($sql, $params = Array ()) {$result = ';      try {$stm = $this->dbh->prepare ($sql);      if ($stm && $stm->execute ($params)) {$result = $stm->fetchcolumn ();    } return $result;    } catch (Exception $e) {$this->outputerror ($e->getmessage ()); }} publicfunction Insert ($table, $params = Array (), $returnLastId = True) {$_implode_field = ';    $fields = Array_keys ($params);    $_implode_field = Implode (', ', $fields);    $_implode_value = ";    foreach ($fields as $value) {$_implode_value. = ': '. $value. ', ';    } $_implode_value = Trim ($_implode_value, ', '); $sql = ' INSERT into '. $table. ' ('. $_implode_field.    ') VALUES ('. $_implode_value. ') ';      try {$stm = $this->dbh->prepare ($sql);      $result = $stm->execute ($params);      if ($returnLastId) {$result = $this->dbh->lastinsertid ();    } return $result;    } catch (Exception $e) {$this->outputerror ($e->getmessage ());    }} Public Function Update ($table, $params = Array (), $where = null) {$_implode_field = ';    $_implode_field_arr = Array ();    if (empty ($where)) {return false;    } $fields = Array_keys ($params); foreach ($fields as $key) {$_implode_field_arr[] = $key. '=' .  ': '. $key;  } $_implode_field = Implode (', ', $_implode_field_arr); $sql = ' UPDATE '. $table. ' SET '. $_implode_field. ' WHERE '.    $where;      try {$stm = $this->dbh->prepare ($sql);      $result = $stm->execute ($params);    return $result;    } catch (Exception $e) {$this->outputerror ($e->getmessage ());      }} Public Function Delete ($sql, $params = Array ()) {try {$stm = $this->dbh->prepare ($sql);      $result = $stm->execute ($params);    return $result;    } catch (Exception $e) {$this->outputerror ($e->getmessage ());      }} public function exec ($sql, $params = Array ()) {try {$stm = $this->dbh->prepare ($sql);      $result = $stm->execute ($params);    return $result;    } catch (Exception $e) {$this->outputerror ($e->getmessage ()); }} Private Function Outputerror ($STRERRMSG) {throw new Exception ("MySQL Error:").  $STRERRMSG); } public Function __destruct () {$this-&GT;DBH = nulL }}

Instance:

<?phprequire_once './db.class.php '; $pdo = db::getinstance (' 127.0.0.1 ', ' root ', ' 111111 ', ' php_cms '); $sql = "Select ID, title1 from cms_wz where id =: ID limit 1 ", $parame = array (' id ' = +); $res = $pdo->fetchone ($sql, $parame); var _dump ($res); $sql = ' SELECT * from Cms_link '; $result = $db->fetchall ($sql);p rint_r ($result);//query record quantity $sql = ' Select  COUNT (*) from cms_link '; $count = $db->fetchcolumn ($sql); echo $count; $data = Array (' SiteID ' = 1, ' linktype ' = 1, ' name ' + ' Google ', ' url ' = ' http://www.google.com ', ' listorder ' and ' = 0 ', ' elite ' + 0, ' passed ' = 1, ' addtime ' = Time ()), $lastInsertId = $db->insert (' Cms_link ', $data); echo $lastInsertId;//Use try try {$resu   lt = $pdo->insert (' News ', $essay);     } catch (Exception $e) {error_log ($e->getmessage ()); Error_log ($e->getmessage (). ' In '. __file__. ' On line '.     __LINE__); Savelog (' URL article: '. $essay [' link '].     ' Data insertion failure <br> ');   Continue } $data = array(' SiteID ' = 1, ' linktype ' + 1, ' name ' = ' Google ', ' url ' = ' http://www.google.com ', ' listorder ' + 0, ' elite ' = + 0, ' passed ' + 1, ' addtime ' = Time ()), $db->insert (' Cms_link ', $data); $sql = ' DELETE from Cms_link WHERE linkid=4 '; $result = $db->delete ($sql); Var_dump ($result);

Related recommendations:

The transaction processing method of PHP based on PDO

PDO to MySQL database additions and deletions to change the operation of the steps detailed

PHP in the PDO operation MySQL detailed

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.