这是我的代码:
$db = new PDO('mysql:host=192.168.1.1;dbname=mydb');
是否可以更改异常消息"无法连接到'192.168.1.1'上的MySQL服务器"的语言?我用 setlocale(LC_ALL,'ru_RU.utf8') 更改 php 语言环境,它会影响日期
然而,该消息是英文的。
class Cp1251ErrorExeption extends ErrorException {
public function getUtfMessage() {
return iconv('cp1251', 'utf-8', $this->getMessage());
}
function handleError($errno, $errstr, $errfile, $errline, array $errcontext){
if (0 === error_reporting())
return false;
throw new self($errstr, 0, $errno, $errfile, $errline);
}
}
try {
try {
set_error_handler('Cp1251ErrorExeption::handleError');
$db = new PDO('mysql:host=192.168.1.1;dbname=mydb');
} catch (PDOException $e) {
throw new Cp1251ErrorExeption($e->getMessage());
}
} catch (Cp1251ErrorExeption $e) {
echo $e->getUtfMessage();
}
restore_error_handler();
Exception的消息是1:1从mysql获取的。使用选项语言=俄语启动 mysqld。通常,您可以在 [mysqld] 部分的/etc/mysql/my.cnf 中配置它。