<?php
$server="localhost";
$user="root";
$pass="";
$dbname="expert";
$dsn="mysql:host=$server;dbname=$dbname";
try {
$connect=new PDO($dsn,$user,$pass);
$connect->exec("SET character_set_connection ='utf8mb4'");
$connect->exec("SET NAMES ='UTF8'");
}
catch (PDOException $error){
echo ("unable to connect".$error->getMessage());
}
?>
unable to connectSQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ” at line 1
2
Answers
This Worked for me you can try this
NAMES is not a variable, SET NAMES is a SQL command.
According to the documentation of SET NAMES, the command requires a character set and optional a collation. Quotes are optional for the character set or collation clauses, e.g.
However, you should avoid sending extra commands, since client can send the character set already during connection handshake.
This can be done by specifying the character set in your DSN: