Php PDO异常消息的区域设置
这是我的代码:Php PDO异常消息的区域设置,php,mysql,exception,locale,Php,Mysql,Exception,Locale,这是我的代码: $db = new PDO('mysql:host=192.168.1.1;dbname=mydb'); 是否可以更改异常消息“无法连接到'192.168.1.1'上的MySQL服务器”的语言? 我用setlocale(LC_ALL,'ru_ru.utf8')更改php语言环境,它会影响日期 不过,该消息是英文的。Exception的消息以1:1的比例从mysql中获取。使用选项language=俄语启动mysqld。通常,您可以在[mysqld]部分的/etc/mysql/m
$db = new PDO('mysql:host=192.168.1.1;dbname=mydb');
是否可以更改异常消息“无法连接到'192.168.1.1'上的MySQL服务器”的语言?
我用setlocale(LC_ALL,'ru_ru.utf8')更改php语言环境,它会影响日期不过,该消息是英文的。Exception的消息以1:1的比例从mysql中获取。使用选项language=俄语启动mysqld。通常,您可以在[mysqld]部分的/etc/mysql/my.cnf中配置它。可能值得使用mysql或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();