SQLSTATE[HY000] [2002] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。
SQLSTATE[HY000] [2002] 由于目标计算机积极拒绝,无法连接。
SQLSTATE[HY000] [2002] 由于系统缓冲区空间不足或队列已满,不能执行套接字上的操作。
SQLSTATE[HY000] [2002] No connection could be made because the target machine actively refused it.
SQLSTATE[HY000] [2002] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
PHP PDO连接MySQL数据库时,如果MySQL没有启动或者无法访问时将会出现SQLSTATE[HY000] [2002]错误。该错误信息是由PHP所在的计算机系统底层产生的,如果操作系统为Windows并且操作系统语言为简体中文,那么错误信息为GBK编码,PHP使用非GBK编码时会出现乱码,解决方法是将错误信息从GBK转码为UTF-8以后再输出,转码可以使用iconv()函数,该函数需要开启php mbstring扩展以后才能使用,下面是解决 SQLSTATE[HY000] [2002] 乱码的PHP示例代码。- <?php
- try{
- $db = new pdo(
- 'mysql:host=wuxiancheng.cn;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());
- }
- ?>
复制代码 |
|