登录  /  注册

php pdo公共类定义与用法实例分享

小云云
发布: 2018-02-06 10:14:20
原创
976人浏览过

本文主要和大家介绍php实现的pdo公共类定义与用法,结合具体实例形式分析了php实现的pdo操作类定义及查询、插入等使用技巧,需要的朋友可以参考下,希望能帮助到大家。

db.class.php :


<?php
class db extends \PDO {
  private static $_instance = null;
  protected $dbName = &#39;&#39;;
  protected $dsn;
  protected $dbh;
  public function __construct($dbHost, $dbUser, $dbPasswd, $dbName, $dbCharset=&#39;utf8&#39;) {
    try {
      $this->dsn = &#39;mysql:host=&#39; . $dbHost . &#39;;dbname=&#39; . $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(&#39;SET character_set_connection=&#39;.$dbCharset.&#39;;SET character_set_client=&#39;.$dbCharset.&#39;;SET character_set_results=&#39;.$dbCharset);
    } catch (Exception $e) {
      $this->outputError($e->getMessage()); 
    }
  }
  public static function getInstance($dbHost, $dbUser, $dbPasswd, $dbName, $dbCharset=&#39;utf8&#39;) {
    if (self::$_instance === 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 = &#39;&#39;;
    try {
      $stm = $this->dbh->prepare($sql);
      if ($stm && $stm->execute($params)) {
        $result = $stm->fetchColumn();
      }
      return $result;
    } catch (Exception $e) {
      $this->outputError($e->getMessage());
    }
  }
  public function insert($table, $params = array(), $returnLastId = true) {
    $_implode_field = &#39;&#39;;
    $fields = array_keys($params);
    $_implode_field = implode(&#39;,&#39;, $fields);
    $_implode_value = &#39;&#39;;
    foreach ($fields as $value) {
      $_implode_value .= &#39;:&#39;. $value.&#39;,&#39;;
    }
    $_implode_value = trim($_implode_value, &#39;,&#39;);
    $sql = &#39;INSERT INTO &#39; . $table . &#39;(&#39; . $_implode_field . &#39;) VALUES (&#39;.$_implode_value.&#39;)&#39;;
    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 = &#39;&#39;;
    $_implode_field_arr = array();
    if ( empty($where) ) {
      return false;
    }
    $fields = array_keys($params);
    foreach ($fields as $key) {
      $_implode_field_arr[] = $key . &#39;=&#39; . &#39;:&#39;.$key;
    }
    $_implode_field = implode(&#39;,&#39;, $_implode_field_arr);
    $sql = &#39;UPDATE &#39; . $table . &#39; SET &#39; . $_implode_field . &#39; WHERE &#39; . $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->dbh = null;
  }
}
登录后复制

实例:


<?php
require_once &#39;./db.class.php&#39;;
$pdo = db::getInstance(&#39;127.0.0.1&#39;, &#39;root&#39;, &#39;111111&#39;, &#39;php_cms&#39;);
$sql = "select id, title1 from cms_wz where id = :id limit 1";
$parame = array(&#39;id&#39; => 12,);
$res = $pdo->fetchOne($sql, $parame);
var_dump($res);
$sql = &#39;SELECT * FROM cms_link&#39;;
$result = $db->fetchAll($sql);
print_r($result);
//查询记录数量
$sql = &#39;SELECT COUNT(*) FROM cms_link&#39;;
$count = $db->fetchColumn($sql);
echo $count;
$data = array(
  &#39;siteid&#39; => 1,
  &#39;linktype&#39; => 1,
  &#39;name&#39; => &#39;google&#39;,
  &#39;url&#39; => &#39;http://www.google.com&#39;,
  &#39;listorder&#39; => 0,
  &#39;elite&#39; => 0,
  &#39;passed&#39; => 1,
  &#39;addtime&#39; => time()
  );
$lastInsertId = $db->insert(&#39;cms_link&#39;, $data);
echo $lastInsertId;
//用 try
 try {
     $result = $pdo->insert(&#39;news&#39;, $essay);
   } catch (Exception $e) {
     error_log($e->getMessage());
     error_log($e->getMessage() . &#39; in &#39; . __FILE__ . &#39; on line &#39; . __LINE__);
     saveLog(&#39;url文章 : &#39; . $essay[&#39;link&#39;] . &#39;  数据插入失败<br>&#39;);
     continue;
   }
$data = array(
  &#39;siteid&#39; => 1,
  &#39;linktype&#39; => 1,
  &#39;name&#39; => &#39;google&#39;,
  &#39;url&#39; => &#39;http://www.google.com&#39;,
  &#39;listorder&#39; => 0,
  &#39;elite&#39; => 0,
  &#39;passed&#39; => 1,
  &#39;addtime&#39; => time()
  );
$db->insert(&#39;cms_link&#39;, $data);
$sql = &#39;DELETE FROM cms_link WHERE linkid=4&#39;;
$result = $db->delete($sql);
var_dump($result);
登录后复制

相关推荐:

PHP之详解PDO

PHP中有关PDO数据访问抽象层的操作

php基于pdo如何实现事务处理的示例代码分享

以上就是php pdo公共类定义与用法实例分享的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
相关标签:
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号