UTF8 in PHP MySQL connection

0

I have a PHP PDO connection with two databases, however, I'm having problems with the special characters of both. How to solve?

$host = "localhost";
$user  = "user";
$password =  "";
$database1 = "bd1";
$database2 = "bd2";

try {
    $dbh1 = new PDO("mysql:host=$host;dbname=$database1", $user, $password);
} catch(PDOException $e) {
    die('Could not connect to the database:' . $e);
}

try {
    $dbh2 = new PDO("mysql:host=$host;dbname=$database2", $user, $password);
} catch(PDOException $e) {
    die('Could not connect to the database:' . $e);
}
?>
    
asked by anonymous 30.01.2017 / 21:58

2 answers

3
//observe o charset=utf8 na DSN
try {
    $dbh1 = new PDO("mysql:host=$host;dbname=$database1;charset=utf8", $user, $password);
} catch(PDOException $e) {
    die('Could not connect to the database:' . $e);
}
    
30.01.2017 / 22:02
0

I had the same problem and I resolved by changing the my.ini file, adding the two lines below

[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci

link

    
17.07.2017 / 22:13