DAOMySQLi类,方便与数据库交互

<?php
final class DAOMySQLi
{
    //将 <成员属性> 以 _ 开头是一种命名风格
    private $_host;
    private $_user;
    private $_pwd;
    private $_dbname;
    private $_port;
    private $_charset;
    //标识DAOMySQLi的一个对象实例
    private static $_instance;
    //连接
    private $_mysqli;

    /*
     * 构造方法
     */
    private function __construct(array $option)
    {
        //验证数据
        $this->_host = isset($option['host']) ? $option['host'] : '';
        $this->_user = isset($option['user']) ? $option['user'] : '';
        $this->_pwd = isset($option['pwd']) ? $option['pwd'] : '';
        $this->_dbname = isset($option['dbname']) ? $option['dbname'] : '';
        $this->_port = isset($option['port']) ? $option['port'] : '';
        $this->_charset = isset($option['charset']) ? $option['charset'] : '';
        if($this->_host == '' || $this->_user == ''|| $this->_dbname == '' || $this->_pwd == '' || $this->_port == '' || $this->_charset == ''){
            die("传入参数有误<br>");
        }
        //初始化 _mysqli
        $this->_mysqli = new mysqli($this->_host, $this->_user, $this->_pwd, $this->_dbname, $this->_port);
        if ($this->_mysqli->connect_errno){
            die('连接失败,错误原因:'.$this->_mysqli->connect_error);
        }
        //设置字符集
        $this->_mysqli->set_charset($this->_charset);
    }

    //定义一个静态方法
    public static function getSingleton(array $option){
        //判断是否已经有对象实例
        //如果没有创建对象实例 就创建一个
        if(!self::$_instance instanceof self){
            self::$_instance = new self($option);
            return self::$_instance;
        }
    }

    /*
     *防止克隆
     */
    private function __clone()
    {
        // TODO: Implement __clone() method.}
    }

    /*
     *对数据库的查询方法
     */
    //查询返回多条结果
    public function fetchAll($sql){
        $arr = array();
        if($res = $this->_mysqli->query($sql)){
            //细节:创建了的结果集要尽快释放,所以在这里将结果集转换成数组 释放结果集 返回数组
            while ($row = $res->fetch_assoc()){
                $arr[] = $row;
            }
            $res->free();
            return $arr;
        }else{
            echo '<br> 执行失败  sql语句是'. $sql;
            echo '<br> 失败原因:'. $this->_mysqli->error;
            exit();
        }
    }
    //查询返回一条结果
    public function fetchOne($sql){
        if($res = $this->_mysqli->query($sql)){
            $arr = $res->fetch_assoc();
        }else{
            echo '<br> 执行失败  sql语句是'. $sql;
            echo '<br> 失败原因:'. $this->_mysqli->error;
            exit();
        }
        $res->free();
        return $arr;
    }
    /*
     * 对表的dml操作
     */
    public function query($sql){
        if($this->_mysqli->query($sql)){
            return true;
        }else{
            echo '<br> 执行失败  sql语句是'. $sql;
            echo '<br> 失败原因:'. $this->_mysqli->error;
            exit();
        }
    }
    /*
     * 查找最新插入的商品id
     */
    public function queryMaxId(){
        $sql = 'SELECT MAX(id) AS maxid FROM Goods';
        $res = $this->_mysqli->query($sql);
        if($row = $res->fetch_assoc()){
            $maxid = $row['maxid'];
        }else{
            echo"最新插入的商品id查找失败";
        }
        return $maxid;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值