Db.class.php:
<?php Class DB extends \pdo {private static $_instance = null;
protected $dbName = ';
protected $dsn;
protected $DBH; Public function __construct ($dbHost, $dbUser, $dbPasswd, $dbName, $dbCharset = ' utf8 ') {try {$this ;d sn = ' mysql:host= '. $dbHost. ';d bname= '.
$dbName;
$this->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);
The catch (Exception $e) {$this->outputerror ($e->getmessage ()); The public static function getinstance ($dbHost, $dbUser, $dbPasswd, $dbName, $dbCharset = ' utf8 ') {if (sel F::$_instance = = null) {Self::$_instancE = new self ($dbHost, $dbUser, $dbPasswd, $dbName, $dbCharset);
return self::$_instance; The Public Function Fetchall ($sql, $params = Array ()) {try {$stm = $this->dbh->prepare ($sql
);
if ($stm && $stm->execute ($params)) {return $stm->fetchall (\PDO::FETCH_ASSOC);
The catch (Exception $e) {$this->outputerror ($e->getmessage ());
The 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;
The catch (Exception $e) {$this->outputerror ($e->getmessage ());
The Public Function Fetchcolumn ($sql, $params = Array ()) {$result = ';
try { $stm = $this->dbh->prepare ($sql);
if ($stm && $stm->execute ($params)) {$result = $stm->fetchcolumn ();
return $result;
The catch (Exception $e) {$this->outputerror ($e->getmessage ());
The Public function 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;
The catch (Exception $e) {$this->outputerror ($e->getmessage ());
The 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;
The catch (Exception $e) {$this->outputerror ($e->getmessage ()); The Public function Delete ($sql, $params = Array ()) {try {$stm = $this->dbh->prePare ($sql);
$result = $stm->execute ($params);
return $result;
The catch (Exception $e) {$this->outputerror ($e->getmessage ()); The public function exec ($sql, $params = Array ()) {try {$stm = $this->dbh->prepare ($
SQL);
$result = $stm->execute ($params);
return $result;
The catch (Exception $e) {$this->outputerror ($e->getmessage ()); The Private Function Outputerror ($STRERRMSG) {throw new Exception ("MySQL Error:".)
$STRERRMSG);
The Public Function __destruct () {$this->dbh = null;
Self::$_instance = null; }
}
instance:
<?php require_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 ' => 12,);
$res = $pdo->fetchone ($sql, $parame);
Var_dump ($res);
$sql = ' SELECT * from Cms_link ';
$result = $db->fetchall ($sql);
Print_r ($result);
Number of query Records $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 ' => 0, ' elite ' => 0, ' passed ' => 1, ' Addtime ' => time ());
$lastInsertId = $db->insert (' Cms_link ', $data);
Echo $lastInsertId;
With the try try {$result = $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.goo ')
Gle.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);