php 封装mysql单例

php 封装mysql单例

<?php
header("Content-Type: text/html;charset=utf-8");

//实现单例
class MySQLDB
{
    private $host; //主机地址
    private $port; //端口号
    private $user; //用户名
    private $pwd; //密码
    private $dbname; //数据库名
    private $charset; //字符集
    private $link;//连接对象


    private static $instance;


    private function __construct($param)
    {
        $this->initParam($param);
        $this->initConnect();

    }

    private function __clone()
    {

    }

    public static function getInstance($param = array())
    {
        if (!self::$instance instanceof self) {
            self::$instance = new self($param);
        }
        return self::$instance;
    }

    private function initParam($param)
    {
        $this->host = $param['host'] ?? '127.0.0.1';
        $this->port = $param['port'] ?? '3306';
        $this->user = $param['user'] ?? '';
        $this->pwd = $param['pwd'] ?? '';
        $this->dbname = $param['dbname'] ?? '';
        $this->charset = $param['charset'] ?? 'utf8';
    }

    private function initConnect()
    {
        $this->link = @mysqli_connect($this->host, $this->user, $this->pwd, $this->dbname);
        if (mysqli_connect_error()) {
            echo '数据库连接失败<br>';
            echo '错误信息:' . mysqli_connect_error(), '<br>';
            echo '错误码:' . mysqli_connect_errno(), '<br>';
            exit;
        }
        mysqli_set_charset($this->link, $this->charset);
    }

    private function execute($sql)
    {
        if (!$rs = mysqli_query($this->link, $sql)) {
            echo 'SQL语句执行失败<br>';
            echo '错误信息:' . mysqli_error($this->link), '<br>';
            echo '错误码:' . mysqli_errno($this->link), '<br>';
            echo '错误的SQL语句:' . $sql . '<br>';
            exit;
        } else {
            return $rs;
        }
    }

    public function exec($sql)
    {
        $key = substr($sql, 0, 6);
        if (in_array($key, array('insert', 'update', 'delete'))) {
            return $this->execute($sql);
        } else {
            echo '非法访问';
            exit;
        }
    }

    public function getLastInsertId()
    {
        return mysqli_insert_id($this->link);
    }

    //查询多条数据,返回二维数组
    public function fetchAll($sql, $type = 'assoc')
    {
        $rs = $this->query($sql);
        $type = $this->getType($type);
        if ($rs) {
            return mysqli_fetch_all($rs, $type);
        }
    }

    //匹配一维数组
    public function fetchRow($sql, $type = 'assoc')
    {
        $list = $this->fetchAll($sql, $type);

        if (count($list) > 0) {
            return $list[0];
        }else{
            echo '没有查询结果';
        }

    }

    //匹配一行一列
    public function fetchColum($sql)
    {
        $list = $this->fetchRow($sql,'num');
        if (!empty($list)) {
            return $list[0];
        }
        return null;
    }

    //查询方法
    public function query($sql)
    {
        if (substr($sql, 0, 6) == 'select' || substr($sql, 0, 4) == 'show' || substr($sql, 0, 4) == 'desc') {
            return $this->execute($sql);
        } else {
            echo '非法访问';
            exit;
        }
    }

    //获取匹配的类型
    private function getType($type)
    {
        switch ($type) {
            case 'assoc':
                return MYSQLI_ASSOC;
            case 'num':
                return MYSQLI_NUM;
            case 'both':
                return MYSQLI_BOTH;
            default:
                return MYSQLI_ASSOC;
        }

    }
}


//测试
//配置参数
$param = array(
    'user' => 'root',
    'pwd' => '123456',
    'dbname' => 'test11'
);
//获取单例
$db = MySQLDB::getInstance($param);
//if ($db->exec("insert into user values (null,'admin1222','123456','18025337866')")) {
//    echo 21;
//    echo "编号是" . $db->getLastInsertId();
//}

//echo $db->exec("insert into user values (null,'admin11222','123456','18025337866')");

//$rs = $db->fetchColum("select user from user where id='37'");

echo "<pre>";
//echo $rs;

//print_r($rs);

采用单例设计模式,只允许实例化一个mysqldb对象,封装了mysqli的增删改查的方法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值