0

This is my code:

$db = new PDO('mysql:host=192.168.1.1;dbname=mydb');

Is it possible to change language of the Exception's message "Can't connect to MySQL server on '192.168.1.1'"? I change php locale with setlocale(LC_ALL, 'ru_RU.utf8') and it affects on dates
nevertheless the message is in English.

4

2 回答 2

0
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();
于 2013-11-22T15:01:54.383 回答
0

异常的消息是从 mysql 中 1:1 获取的。使用选项 language=russian 启动 mysqld。通常,您可以在 [mysqld] 部分的 /etc/mysql/my.cnf 中配置它。

于 2015-05-19T23:07:45.623 回答