UTF8 on Mysql PHP connection

Asked

Viewed 1,096 times

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);
}
?>

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);
}
  • It worked out buddy. Mt thanks.

0

Browser other questions tagged

You are not signed in. Login or sign up in order to post.