zoukankan      html  css  js  c++  java
  • mysqli单例模式连接数据库

    <?php
    define("HOST",'localhost');
    define("USERNAME",'root');
    define("PASSWORD",'root');
    define("DBNAME",'dianying');
    //单例模式连接数据库  使用mysqli
    class lianjie
    {
        private static $instance;
        private static $config;
        private $dbase = [
            'host' => HOST,
            'username' => USERNAME,
            'password' => PASSWORD,
            'dbname' => DBNAME,
        ];
    
        private function __construct()
        {
        }
    
        private function __clone()
        {
        }
    
        public static function getInstance()
        {
            if (!self::$instance instanceof self) {
                self::$instance = new self();
            }
    
            return self::$instance;
        }
    
        public function conn()
        {
            self::$config = new mysqli($this->dbase['host'], $this->dbase['username'], $this->dbase['password']);
            self::$config->query('set name utf8');
            self::$config->select_db($this->dbase['dbname']);
            return self::$config;
        }
    
    }
    $obj = lianjie::getInstance();
    $db = $obj->conn();
    // $sql = 'select * from xinwen LIMIT 10';
    // $row = $db->query($sql);
    // $data = [];
    // while ($tmp = $row->fetch_assoc()) {
    //     $data[] = $tmp;
    // }
    // echo '<pre>';
    // print_r($data);
    // echo '</pre>';
  • 相关阅读:
    MAVEN学习笔记之私服Nexus(2)
    MAVEN学习笔记之基础(1)
    mybatis 高级映射和spring整合之逆向工程(7)
    IPC之共享内存
    IPC之SystemV
    IPC之消息队列
    IPC之信号量
    线程同步
    线程函数
    线程基础
  • 原文地址:https://www.cnblogs.com/mengor/p/13300815.html
Copyright © 2011-2022 走看看