Reputation: 243
I am migrating my classes but I am getting this error in laravel.
[PDOException]
SQLSTATE[42000]: Syntax error or access violation: 1115 Unknown character set: 'utf8mb4'
[PDOException]
SQLSTATE[42000]: Syntax error or access violation: 1115 Unknown character set: 'utf8mb4'
How do I resolve this error?
Upvotes: 15
Views: 27415
Reputation: 21
If that doesn't work, you can also replace in /libs/database.php the old function
connect():
private function connect(){
if(!isset($_SESSION["db_host"]) & !isset($_SESSION["db_username"]) & !isset($_SESSION["db_pass"]) & !isset($_SESSION["db_name"])){
$host = DB_HOST;
$user = DB_USER;
$pass = DB_PASS;
$db_name = DB_NAME;
}else{
$host = $_SESSION["db_host"];
$user = $_SESSION["db_username"];
$pass = $_SESSION["db_pass"];
$db_name = $_SESSION["db_name"];
}
try{
// $this->con = new PDO("mysql:host=$host;dbname=$db_name", $user, $pass);
$this->con = new PDO("mysql:host=$host;dbname=$db_name", $user, $pass,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8' COLLATE 'utf8_unicode_ci'"));
$this->con->exec("SET CHARACTER SET UTF-8");
$this->con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}catch(PDOException $ex){
echo "Database Connection Error Is: " . $ex->getMessage();
}
}
with this better one:
private function connect(){
if(!isset($_SESSION["db_host"]) & !isset($_SESSION["db_username"]) & !isset($_SESSION["db_pass"]) & !isset($_SESSION["db_name"])){
$host = DB_HOST;
$user = DB_USER;
$pass = DB_PASS;
$db_name = DB_NAME;
} else {
$host = $_SESSION["db_host"];
$user = $_SESSION["db_username"];
$pass = $_SESSION["db_pass"];
$db_name = $_SESSION["db_name"];
}
try {
$this->con = new PDO("mysql:host=$host;dbname=$db_name", $user, $pass, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8' COLLATE 'utf8_unicode_ci'"));
$this->con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $ex) {
echo "Database Connection Error Is: " . $ex->getMessage();
}
}
Upvotes: 0
Reputation: 1
Edit your config/database.php file
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
run -> php artisan config:clear
Upvotes: -2
Reputation: 887
This can be fixed in two ways:
1st - edit your .env file
DB_CHARSET=utf8
DB_COLLATION=utf8_unicode_ci
2nd - If you don't use env-file just edit config/database.php
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
Will work for Lumen/Laravel
Upvotes: -1
Reputation: 1297
Go to config/database.php and replace these lines. That's it
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
Upvotes: 7
Reputation: 2817
Go to config/database.php
and replace these two lines with these
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
Upvotes: 58