05月28, 2020
收藏本站

PHP下的MySQL数据库驱动类 - DbMysql.class.php

先上段php代码,摘录自liu21st的DbMysql.class.php

    /**
     * 连接数据库方法
     * @access public
     * @throws ThinkExecption
     */
    public function connect($config='',$linkNum=0,$force=false) {
        if ( !isset($this->linkID[$linkNum]) ) {
            if(empty($config))  $config =   $this->config;
            // 处理不带端口号的socket连接情况
            $host = $config['hostname'].($config['hostport']?":{$config['hostport']}":'');
            // 是否长连接
            $pconnect   = !empty($config['params']['persist'])? $config['params']['persist']:$this->pconnect;
            if($pconnect) {
                $this->linkID[$linkNum] = mysql_pconnect( $host, $config['username'], $config['password'],131072);
            }else{
                $this->linkID[$linkNum] = mysql_connect( $host, $config['username'], $config['password'],true,131072);
            }
            if ( !$this->linkID[$linkNum] || (!empty($config['database']) && !mysql_select_db($config['database'], $this->linkID[$linkNum])) ) {
                throw_exception(mysql_error());
            }


            $dbVersion = mysql_get_server_info($this->linkID[$linkNum]);
            //使用UTF8存取数据库
            mysql_query("SET NAMES '".C('DB_CHARSET')."'", $this->linkID[$linkNum]);
            //设置 sql_model
            if($dbVersion >'5.0.1'){
                mysql_query("SET sql_mode=''",$this->linkID[$linkNum]);
            }
            // 标记连接成功
            $this->connected    =   true;
            // 注销数据库连接配置信息
            if(1 != C('DB_DEPLOY_TYPE')) unset($this->config);
        }
        return $this->linkID[$linkNum];
    }

每次连接mysql,会先执行set name为配置的字符集,以及set sql_mode=''

但正常我们会在my.cnf配置为sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES!!!

解释一下:

NO_ENGINE_SUBSTITUTION:引擎可用模式,如果建立的表使用的引擎不可用则返回ERROR

STRICT_TRANS_TABLES:严格模式,插入非法的值返回ERROR

因此出现使用该驱动类的php程序,即使插入的数据不合法,也不会报错。

admin test>show create table t_innodb\G
*************************** 1. row ***************************
       Table: t_innodb
Create Table: CREATE TABLE `t_innodb` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) DEFAULT NULL,
  `age` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

admin test>show global variables like 'sql_mode';
+---------------+--------------------------------------------+
| Variable_name | Value                                      |
+---------------+--------------------------------------------+
| sql_mode      | STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION |
+---------------+--------------------------------------------+
1 row in set (0.00 sec)

admin test>insert into t_innodb(name) values('b');
ERROR 1364 (HY000): Field 'age' doesn't have a default value

admin test>set sql_mode='';
Query OK, 0 rows affected (0.00 sec)

admin test>insert into t_innodb(name) values('b');
Query OK, 1 row affected, 1 warning (0.00 sec)

admin test>show warnings;
+---------+------+------------------------------------------+
| Level   | Code | Message                                  |
+---------+------+------------------------------------------+
| Warning | 1364 | Field 'age' doesn't have a default value |
+---------+------+------------------------------------------+
1 row in set (0.00 sec)


Comments