Yongli borrows sqlstate [HY000] [2002] random code solution

永利借呗SQLSTATE[HY000] [2002]乱码解决方案
Yongli borrows sqlstate [HY000] [2002] random code solution

When PHP PDO connects to MySQL database, if MySQL is not started or cannot be accessed, sqlstate [HY000] [2002] error will appear. The error message is generated by the bottom layer of the computer system where PHP is located. If the operating system is windows and the operating system language is simplified Chinese, the error message is GBK code. When PHP uses non GBK code, there will be garbled code. The solution is to transcode the error message from GBK to UTF-8 and then output it. The transcoding can use iconv() function, which needs to turn on PHP Mbstring can only be used after extension. Here is the PHP example code to solve SQL state [HY000] [2002] garbled code. Just open the extension in php.ini

<?php
        try{
                $db = new pdo(
                        'mysql:host=neapi.com;port=3306;dbname=mysql;charset=utf8',
                        'user',
                        'password',
                        array(
                                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
                                PDO::ATTR_TIMEOUT => 3
                        )
                );
        }catch(PDOException $e){
                echo $e->getMessage();
                echo '<br />';
                echo iconv('gbk', 'utf-8', $e->getMessage());
        }
?

comment0

请先

Login / registration
Administrator Login