PHP连接和操作mysql数据库的工具类(单例模式)

直接上代码MySqlDBSingle.class.php

host = isset($config['host']) ? $config['host'] : '';
        $this->port = isset($config['port']) ? $config['port'] : '';
        $this->user = isset($config['user']) ? $config['user'] : '';
        $this->pwd = isset($config['pwd']) ? $config['pwd'] : '';
        $this->charset = isset($config['charset']) ? $config['charset'] : 'utf8';
        $this->dbname = isset($config['dbname']) ? $config['dbname'] : '';

    }

    /**
     * 连接数据库
     */
    private function connect()
    {
        $this->link = @mysql_connect("{$this->host}:{$this->port}", $this->user, $this->pwd) or die('数据库连接失败');
    }

    /**
     * 此方法用来执行sql语句
     * 如果是数据查询语句 成功返回结果集 失败返回false
     * 如果是数据操作语句 成功返回true   失败返回false
     * @param $sql
     * @return resource
     */
    public function query($sql)
    {
        if (!$result = mysql_query($sql, $this->link)) {
            echo 'sql语句执行失败
'; echo '错误编号:' . mysql_errno(), '
'; echo '错误信息:' . mysql_error(), '
'; echo '错误的sql语句:' . $sql, '
'; exit; } return $result; } /** * 设置字符集编码 */ private function setCharset() { $sql = "set names {$this->charset}"; $this->query($sql); } /** * 选择数据库 */ private function selectDB() { $sql = "use {$this->dbname}"; $this->query($sql); } /** * 私有的构造方法防止类的外部实例化对象 * MySqlDBSingle constructor. * @param $config 连接数据库的参数 */ private function __construct($config) { $this->initParams($config); $this->connect(); $this->setCharset(); $this->selectDB(); } /** * 私有的克隆方法 用来阻止类的外部克隆对象 */ private function __clone() { // TODO: Implement __clone() method. } /** * 公有的静态方法获取唯一的实例 * @param array $config * @return MySqlDBSingle */ public static function getInstance($config = array()) { if (!self::$instance instanceof MySqlDBSingle) { self::$instance = new MySqlDBSingle($config); } return self::$instance; } /** * 从数据库获取所有数据 * @param $sql * @param string $fetch_type assoc|row|array * @return array 将表中的数据匹配成二维数组返回 */ public function fetchAll($sql, $fetch_type = 'assoc') { $rs = $this->query($sql); $fetch_types = array('assoc','row','array'); if (!in_array($fetch_type,$fetch_types)) { $fetch_type = 'assoc'; } $fetch_fun = 'mysql_f etch_'.$fetch_type; $array = array(); while ($rows = $fetch_fun($rs)) { $array[] = $rows; } return $array; } /** * 查询列表的第一条记录 * @param $sql * @param string $fetch_type * @return mixed|null */ public function fetchRow($sql, $fetch_type = 'assoc') { $rs = $this->fetchAll($sql, $fetch_type); if(!empty($rs)) { return $rs[0]; } return null; } /** * 查询列表的第一行第一列 * @param $sql * @return null */ public function fetchColumn($sql) { $rs = $this->fetchRow($sql,'row'); if(!empty($rs)) { return $rs[0]; } return null; } } $config = array( 'host' => 'localhost', 'port' => '3306', 'user' => 'root', 'pwd' => 'root', 'charset' => 'utf8', 'dbname' => 'mydatabase' ); $db = MySqlDBSingle::getInstance($config); $rs = $db->fetchAll("select * from my_student",'assoc'); echo '
';
var_dump($rs);echo '
'; $rs = $db->fetchRow("select * from my_student"); echo '
';
var_dump($rs);echo '
'; $rs = $db->fetchColumn("select * from my_student"); echo '
';
var_dump($rs);echo '
';

你可能感兴趣的:(PHP连接和操作mysql数据库的工具类(单例模式))