{"id":455,"date":"2020-11-26T10:35:58","date_gmt":"2020-11-26T02:35:58","guid":{"rendered":"http:\/\/blog.doublejia.cn\/?p=455"},"modified":"2020-11-26T10:35:58","modified_gmt":"2020-11-26T02:35:58","slug":"thinkphp-3-%e9%81%87%e5%88%b0mysql-2006-%e6%8a%a5%e9%94%99","status":"publish","type":"post","link":"https:\/\/blog.doublejia.cn\/?p=455","title":{"rendered":"ThinkPHP 3 \u9047\u5230mysql 2006 \u62a5\u9519"},"content":{"rendered":"\n<p>\u516c\u53f8\u9879\u76ee\uff0c\u4f7f\u7528\u7684 ThinkPHP 3\u6846\u67b6\uff0c\u9047\u5230\u4e2amysql \u7684\u62a5\u9519\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>2006 MySQL server has gone away<\/code><\/pre>\n\n\n\n<p>\u4e00\u987f\u641c\u7d22\u4e4b\u540e\uff08https:\/\/blog.csdn.net\/xiaomei_haimi\/article\/details\/81087037\uff09\uff0c\u5728\u67e5\u770b show global variables like '%timeout'; \u65f6\u53d1\u73b0\uff0cwait_timeout \u4e3a 4 \u79d2\u3002\u52a0\u5165\u4e00\u4e9b\u68c0\u67e5\u540e\u53d1\u73b0\uff0c\u62a5\u9519\u7684 SQL\uff0c\u6709\u4e4b\u524d\u7684\u4e00\u4e2a\u76f8\u5dee\u4e86 5~6 \u79d2\u3002\u95ee\u9898\u627e\u5230\uff0c\u5f00\u59cb\u89e3\u51b3\u95ee\u9898\u3002<\/p>\n\n\n\n<p>\u60f3\u5230\u7684\u529e\u6cd5\u662f\uff0c\u5728\u53d1\u73b0mysql \u62a5 2006 \u4e4b\u540e\uff0c\u6a21\u4eff TP 5\u4e2d\u7684\u4f5c\u6cd5\uff0c\u52a0\u5165\u91cd\u8fde\u3002  \u5728 Library\\Think\\Db\\Driver\\Mysqli.class.php \u4e2d\uff0cquery \u548c excute \u65b9\u6cd5\u8fdb\u884c\u4fee\u6539\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\/\/ +----------------------------------------------------------------------\n\/\/ | ThinkPHP &#091; WE CAN DO IT JUST THINK IT ]\n\/\/ +----------------------------------------------------------------------\n\/\/ | Copyright (c) 2007 http:\/\/thinkphp.cn All rights reserved.\n\/\/ +----------------------------------------------------------------------\n\/\/ | Licensed ( http:\/\/www.apache.org\/licenses\/LICENSE-2.0 )\n\/\/ +----------------------------------------------------------------------\n\/\/ | Author: liu21st &lt;liu21st@gmail.com&gt;\n\/\/ +----------------------------------------------------------------------\nnamespace Think\\Db\\Driver;\nuse Think\\Db;\ndefined('THINK_PATH') or exit();\n\/**\n * Mysqli\u6570\u636e\u5e93\u9a71\u52a8\u7c7b\n *\/\nclass Mysqli extends Db{\n\t\n\tprotected $isBreak = false;\n\t\n\t\n\n    \/**\n     * \u67b6\u6784\u51fd\u6570 \u8bfb\u53d6\u6570\u636e\u5e93\u914d\u7f6e\u4fe1\u606f\n     * @access public\n     * @param array $config \u6570\u636e\u5e93\u914d\u7f6e\u6570\u7ec4\n     *\/\n    public function __construct($config=''){\n        if ( !extension_loaded('mysqli') ) {\n            E(L('_NOT_SUPPERT_').':mysqli');\n        }\n        if(!empty($config)) {\n            $this-&gt;config   =   $config;\n            if(empty($this-&gt;config&#091;'params'])) {\n                $this-&gt;config&#091;'params'] =   '';\n            }\n        }\n    }\n\n    \/**\n     * \u8fde\u63a5\u6570\u636e\u5e93\u65b9\u6cd5\n     * @access public\n     * @throws ThinkExecption\n     *\/\n    public function connect($config='',$linkNum=0) {\n        if ( !isset($this-&gt;linkID&#091;$linkNum]) ) {\n            if(empty($config))  $config =   $this-&gt;config;\n            $this-&gt;linkID&#091;$linkNum] = new \\mysqli($config&#091;'hostname'],$config&#091;'username'],$config&#091;'password'],$config&#091;'database'],$config&#091;'hostport']?intval($config&#091;'hostport']):3306);\n            if (mysqli_connect_errno()) E(mysqli_connect_error());\n            $dbVersion = $this-&gt;linkID&#091;$linkNum]-&gt;server_version;\n            \n            \/\/ \u8bbe\u7f6e\u6570\u636e\u5e93\u7f16\u7801\n            $this-&gt;linkID&#091;$linkNum]-&gt;query(\"SET NAMES '\".$config&#091;'charset'].\"'\");\n            \/\/\u8bbe\u7f6e sql_model\n            if($dbVersion &gt;'5.0.1'){\n                $this-&gt;linkID&#091;$linkNum]-&gt;query(\"SET sql_mode=''\");\n            }\n            \/\/ \u6807\u8bb0\u8fde\u63a5\u6210\u529f\n            $this-&gt;connected    =   true;\n            \/\/\u6ce8\u9500\u6570\u636e\u5e93\u5b89\u5168\u4fe1\u606f\n            if(1 != C('DB_DEPLOY_TYPE')) unset($this-&gt;config);\n        }\n        return $this-&gt;linkID&#091;$linkNum];\n    }\n\n    \/**\n     * \u91ca\u653e\u67e5\u8be2\u7ed3\u679c\n     * @access public\n     *\/\n    public function free() {\n        if(is_object($this-&gt;queryID)){\n            $this-&gt;queryID-&gt;free_result();\n        }\n        $this-&gt;queryID = null;\n    }\n\n    \/**\n     * \u6267\u884c\u67e5\u8be2 \u8fd4\u56de\u6570\u636e\u96c6\n     * @access public\n     * @param string $str  sql\u6307\u4ee4\n     * @return mixed\n     *\/\n    public function query($str) {\n        $this-&gt;initConnect(false);\n        if ( !$this-&gt;_linkID ) return false;\n        $this-&gt;queryStr = $str;\n        \/\/\u91ca\u653e\u524d\u6b21\u7684\u67e5\u8be2\u7ed3\u679c\n        if ( $this-&gt;queryID ) $this-&gt;free();\n        N('db_query',1);\n        \/\/ \u8bb0\u5f55\u5f00\u59cb\u6267\u884c\u65f6\u95f4\n        G('queryStartTime');\n        $this-&gt;queryID = $this-&gt;_linkID-&gt;query($str);\n        \/\/ \u5bf9\u5b58\u50a8\u8fc7\u7a0b\u6539\u8fdb\n        if( $this-&gt;_linkID-&gt;more_results() ){\n            while (($res = $this-&gt;_linkID-&gt;next_result()) != NULL) {\n                $res-&gt;free_result();\n            }\n        }\n        $this-&gt;debug();\n        if ( false === $this-&gt;queryID ) {\n\t\t\tif ($this-&gt;_linkID-&gt;errno == 2006 &amp;&amp; !$this-&gt;isBreak){\n\t\t\t\t$this-&gt;isBreak = true;\n\t\t\t\t$this-&gt;close();\n\t\t\t\treturn $this-&gt;query($str);\n\t\t\t}\n            $this-&gt;error();\n            return false;\n        } else {\n            $this-&gt;numRows  = $this-&gt;queryID-&gt;num_rows;\n            $this-&gt;numCols    = $this-&gt;queryID-&gt;field_count;\n            return $this-&gt;getAll();\n        }\n    }\n\n    \/**\n     * \u6267\u884c\u8bed\u53e5\n     * @access public\n     * @param string $str  sql\u6307\u4ee4\n     * @return integer\n     *\/\n    public function execute($str) {\n        $this-&gt;initConnect(true);\n        if ( !$this-&gt;_linkID ) return false;\n        $this-&gt;queryStr = $str;\n        \/\/\u91ca\u653e\u524d\u6b21\u7684\u67e5\u8be2\u7ed3\u679c\n        if ( $this-&gt;queryID ) $this-&gt;free();\n        N('db_write',1);\n        \/\/ \u8bb0\u5f55\u5f00\u59cb\u6267\u884c\u65f6\u95f4\n        G('queryStartTime');\n        $result =   $this-&gt;_linkID-&gt;query($str);\n        $this-&gt;debug();\n        if ( false === $result ) {\n\t\t\tif ($this-&gt;_linkID-&gt;errno == 2006 &amp;&amp; !$this-&gt;isBreak){\n\t\t\t\t$this-&gt;isBreak = true;\n\t\t\t\t$this-&gt;close();\n\t\t\t\treturn $this-&gt;execute($str);\n\t\t\t}\n            $this-&gt;error();\n            return false;\n        } else {\n            $this-&gt;numRows = $this-&gt;_linkID-&gt;affected_rows;\n            $this-&gt;lastInsID = $this-&gt;_linkID-&gt;insert_id;\n            return $this-&gt;numRows;\n        }\n    }\n\n    \/**\n     * \u542f\u52a8\u4e8b\u52a1\n     * @access public\n     * @return void\n     *\/\n    public function startTrans() {\n        $this-&gt;initConnect(true);\n        \/\/\u6570\u636erollback \u652f\u6301\n        if ($this-&gt;transTimes == 0) {\n            $this-&gt;_linkID-&gt;autocommit(false);\n        }\n        $this-&gt;transTimes++;\n        return ;\n    }\n\n    \/**\n     * \u7528\u4e8e\u975e\u81ea\u52a8\u63d0\u4ea4\u72b6\u6001\u4e0b\u9762\u7684\u67e5\u8be2\u63d0\u4ea4\n     * @access public\n     * @return boolen\n     *\/\n    public function commit() {\n        if ($this-&gt;transTimes &gt; 0) {\n            $result = $this-&gt;_linkID-&gt;commit();\n            $this-&gt;_linkID-&gt;autocommit( true);\n            $this-&gt;transTimes = 0;\n            if(!$result){\n                $this-&gt;error();\n                return false;\n            }\n        }\n        return true;\n    }\n\n    \/**\n     * \u4e8b\u52a1\u56de\u6eda\n     * @access public\n     * @return boolen\n     *\/\n    public function rollback() {\n        if ($this-&gt;transTimes &gt; 0) {\n            $result = $this-&gt;_linkID-&gt;rollback();\n            $this-&gt;_linkID-&gt;autocommit( true);\n            $this-&gt;transTimes = 0;\n            if(!$result){\n                $this-&gt;error();\n                return false;\n            }\n        }\n        return true;\n    }\n\n    \/**\n     * \u83b7\u5f97\u6240\u6709\u7684\u67e5\u8be2\u6570\u636e\n     * @access private\n     * @param string $sql  sql\u8bed\u53e5\n     * @return array\n     *\/\n    private function getAll() {\n        \/\/\u8fd4\u56de\u6570\u636e\u96c6\n        $result = array();\n        if($this-&gt;numRows&gt;0) {\n            \/\/\u8fd4\u56de\u6570\u636e\u96c6\n            for($i=0;$i&lt;$this-&gt;numRows ;$i++ ){\n                $result&#091;$i] = $this-&gt;queryID-&gt;fetch_assoc();\n            }\n            $this-&gt;queryID-&gt;data_seek(0);\n        }\n        return $result;\n    }\n\n    \/**\n     * \u53d6\u5f97\u6570\u636e\u8868\u7684\u5b57\u6bb5\u4fe1\u606f\n     * @access public\n     * @return array\n     *\/\n    public function getFields($tableName) {\n        $result =   $this-&gt;query('SHOW COLUMNS FROM '.$this-&gt;parseKey($tableName));\n        $info   =   array();\n        if($result) {\n            foreach ($result as $key =&gt; $val) {\n                $info&#091;$val&#091;'Field']] = array(\n                    'name'    =&gt; $val&#091;'Field'],\n                    'type'    =&gt; $val&#091;'Type'],\n                    'notnull' =&gt; (bool) ($val&#091;'Null'] === ''), \/\/ not null is empty, null is yes\n                    'default' =&gt; $val&#091;'Default'],\n                    'primary' =&gt; (strtolower($val&#091;'Key']) == 'pri'),\n                    'autoinc' =&gt; (strtolower($val&#091;'Extra']) == 'auto_increment'),\n                );\n            }\n        }\n        return $info;\n    }\n\n    \/**\n     * \u53d6\u5f97\u6570\u636e\u8868\u7684\u5b57\u6bb5\u4fe1\u606f\n     * @access public\n     * @return array\n     *\/\n    public function getTables($dbName='') {\n        $sql    = !empty($dbName)?'SHOW TABLES FROM '.$dbName:'SHOW TABLES ';\n        $result =   $this-&gt;query($sql);\n        $info   =   array();\n        if($result) {\n            foreach ($result as $key =&gt; $val) {\n                $info&#091;$key] = current($val);\n            }\n        }\n        return $info;\n    }\n\n    \/**\n     * \u66ff\u6362\u8bb0\u5f55\n     * @access public\n     * @param mixed $data \u6570\u636e\n     * @param array $options \u53c2\u6570\u8868\u8fbe\u5f0f\n     * @return false | integer\n     *\/\n    public function replace($data,$options=array()) {\n        foreach ($data as $key=&gt;$val){\n            $value   =  $this-&gt;parseValue($val);\n            if(is_scalar($value)) { \/\/ \u8fc7\u6ee4\u975e\u6807\u91cf\u6570\u636e\n                $values&#091;]   =  $value;\n                $fields&#091;]   =  $this-&gt;parseKey($key);\n            }\n        }\n        $sql   =  'REPLACE INTO '.$this-&gt;parseTable($options&#091;'table']).' ('.implode(',', $fields).') VALUES ('.implode(',', $values).')';\n        return $this-&gt;execute($sql);\n    }\n\n    \/**\n     * \u63d2\u5165\u8bb0\u5f55\n     * @access public\n     * @param mixed $datas \u6570\u636e\n     * @param array $options \u53c2\u6570\u8868\u8fbe\u5f0f\n     * @param boolean $replace \u662f\u5426replace\n     * @return false | integer\n     *\/\n    public function insertAll($datas,$options=array(),$replace=false) {\n        if(!is_array($datas&#091;0])) return false;\n        $fields = array_keys($datas&#091;0]);\n        array_walk($fields, array($this, 'parseKey'));\n        $values  =  array();\n        foreach ($datas as $data){\n            $value   =  array();\n            foreach ($data as $key=&gt;$val){\n                $val   =  $this-&gt;parseValue($val);\n                if(is_scalar($val)) { \/\/ \u8fc7\u6ee4\u975e\u6807\u91cf\u6570\u636e\n                    $value&#091;]   =  $val;\n                }\n            }\n            $values&#091;]    = '('.implode(',', $value).')';\n        }\n        $sql   =  ($replace?'REPLACE':'INSERT').' INTO '.$this-&gt;parseTable($options&#091;'table']).' ('.implode(',', $fields).') VALUES '.implode(',',$values);\n        return $this-&gt;execute($sql);\n    }\n\n    \/**\n     * \u5173\u95ed\u6570\u636e\u5e93\n     * @access public\n     * @return volid\n     *\/\n    public function close() {\n        if ($this-&gt;_linkID){\n            $this-&gt;_linkID-&gt;close();\n        }\n\t\t$this-&gt;connected = false;\n        $this-&gt;_linkID = null;\n    }\n\n    \/**\n     * \u6570\u636e\u5e93\u9519\u8bef\u4fe1\u606f\n     * \u5e76\u663e\u793a\u5f53\u524d\u7684SQL\u8bed\u53e5\n     * @static\n     * @access public\n     * @return string\n     *\/\n    public function error() {\n        $this-&gt;error = $this-&gt;_linkID-&gt;errno.':'.$this-&gt;_linkID-&gt;error;\n        if('' != $this-&gt;queryStr){\n            $this-&gt;error .= \"\\n &#091; SQL\u8bed\u53e5 ] : \".$this-&gt;queryStr;\n        }\n        trace($this-&gt;error,'','ERR');\n        return $this-&gt;error;\n    }\n\n    \/**\n     * SQL\u6307\u4ee4\u5b89\u5168\u8fc7\u6ee4\n     * @static\n     * @access public\n     * @param string $str  SQL\u6307\u4ee4\n     * @return string\n     *\/\n    public function escapeString($str) {\n        if($this-&gt;_linkID) {\n            return  $this-&gt;_linkID-&gt;real_escape_string($str);\n        }else{\n            return addslashes($str);\n        }\n    }\n\n    \/**\n     * \u5b57\u6bb5\u548c\u8868\u540d\u5904\u7406\u6dfb\u52a0`\n     * @access protected\n     * @param string $key\n     * @return string\n     *\/\n    protected function parseKey(&amp;$key) {\n        $key   =  trim($key);\n        if(!is_numeric($key) &amp;&amp; !preg_match('\/&#091;,\\'\\\"\\*\\(\\)`.\\s]\/',$key)) {\n           $key = '`'.$key.'`';\n        }\n        return $key;\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>\u6d4b\u8bd5\u4e2d\uff0c\u5e94\u8be5\u662f\u53ef\u884c\u7684\u3002:)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u516c\u53f8\u9879\u76ee\uff0c\u4f7f\u7528\u7684 ThinkPHP 3\u6846\u67b6\uff0c\u9047\u5230\u4e2amysql \u7684\u62a5\u9519\uff1a \u4e00\u987f\u641c\u7d22\u4e4b\u540e\uff08https:\/\/blog.csdn.net\/xiaomei_haimi\/article\/details\/81087037\uff09\uff0c\u5728\u67e5\u770b show global variables like &#8216;%timeout&#8217;; \u65f6\u53d1\u73b0\uff0cwait_timeout \u4e3a 4 \u79d2\u3002\u52a0\u5165\u4e00\u4e9b\u68c0\u67e5\u540e\u53d1\u73b0\uff0c\u62a5\u9519\u7684 SQL\uff0c\u6709\u4e4b\u524d\u7684\u4e00\u4e2a\u76f8\u5dee\u4e86 5\u2026<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.doublejia.cn\/index.php?rest_route=\/wp\/v2\/posts\/455"}],"collection":[{"href":"https:\/\/blog.doublejia.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.doublejia.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.doublejia.cn\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.doublejia.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=455"}],"version-history":[{"count":1,"href":"https:\/\/blog.doublejia.cn\/index.php?rest_route=\/wp\/v2\/posts\/455\/revisions"}],"predecessor-version":[{"id":456,"href":"https:\/\/blog.doublejia.cn\/index.php?rest_route=\/wp\/v2\/posts\/455\/revisions\/456"}],"wp:attachment":[{"href":"https:\/\/blog.doublejia.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=455"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.doublejia.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=455"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.doublejia.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=455"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}