用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - php代码库

php 封装PDO 类

2014-11-26 作者: php源代码大全举报

[php]代码库

class PdoHelper {

    private $dsn;
    private $usn;
    private $pwd;
    private $options;

    function __construct($dsn, $usn, $pwd, $options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8')) {
        $this->dsn = $dsn;
        $this->usn = $usn;
        $this->pwd = $pwd;
        $this->options = $options;
    }

    public function GetPDOStatement() {
        return new PDO($this->dsn, $this->usn, $this->pwd, $this->options);
    }

    //获得数据集
    public function GetData($sql, $parames = array()) {
        $dbh = $this->GetPDOStatement();

        if (count($parames) > 0) {
            $stmt = $dbh->prepare($sql);

            $keys = array_keys($parames);

            foreach ($keys as $key) {
                $stmt->bindValue($key, $parames[$key]);
            }

            $stmt->execute();

            return $stmt->fetchAll();
        } else {
            $query = $dbh->query($sql);
            if ($query) {
                return $query->fetchAll();
            }
        }

        return FALSE;
    }

    //获得数据行
    public function GetRow($sql, $parames = array()) {
        $dbh = $this->GetPDOStatement();
        if (count($parames) > 0) {
            $stmt = $dbh->prepare($sql);

            $keys = array_keys($parames);
            foreach ($keys as $key) {
                $stmt->bindValue($key, $parames[$key]);
            }

            $stmt->execute();
            return $stmt->fetch();
        } else {
            $query = $dbh->query($sql);

            if ($query) {
                return $query->fetch();
            }
        }

        return FALSE;
    }

    //获得一个值
    public function GetValue($sql, $parames = array()) {
        $dbh = $this->GetPDOStatement();

        if (count($parames) > 0) {
            $stmt = $dbh->prepare($sql);
            $keys = array_keys($parames);
            foreach ($keys as $key) {
                $stmt->bindValue($key, $parames[$key]);
            }

            $stmt->execute();

            return $stmt->fetchColumn();
        } else {
            $query = $dbh->query($sql);

            if ($query) {
                return $query->fetchColumn();
            }
        }

        return FALSE;
    }

    //执行一条查询
    public function Execute($sql, $parames = array()) {
        $dbh = $this->GetPDOStatement();

        if (count($parames) > 0) {
            $stmt = $dbh->prepare($sql);
            $keys = array_keys($parames);
            foreach ($keys as $key) {
                $stmt->bindValue($key, $parames[$key]);
            }

            return $stmt->execute();
        } else {
            return $dbh->exec($sql);
        }
    }

    //执行插入 并且返回最后插入的ID
    public function InsertDataGetLastID($sql, $parames = array()) {
        $dbh = $this->GetPDOStatement();
        if (count($parames) > 0) {
            $stmt = $dbh->prepare($sql);
            $keys = array_keys($parames);
            foreach ($keys as $key) {
                $stmt->bindValue($key, $parames[$key]);
            }
            $stmt->execute();
            return $dbh->lastInsertId();
        } else {

            $dbh->exec($sql);
            return $dbh->lastInsertId();
        }
    }

}



网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...