mariadb 无法连接到本地主机中的数据库

sh7euo9m  于 2023-02-04  发布在  其他
关注(0)|答案(1)|浏览(175)
<?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());

}
?>

无法连接SQLSTATE [42000]:语法错误或访问冲突:1064 0x0064您的SQL语法有错误;查看与您的MariaDB服务器版本对应的手册,了解在第1行""附近使用的正确语法

5cg8jx4n

5cg8jx4n1#

这个对我有用你可以试试这个

<?php

   $server="localhost";
   $user="root";
   $pass="";

   try {
      $connect=new PDO("mysql:host=$server;dbname=test_db",$user,$pass);
      $connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      $connect->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND , "SET NAMES utf8");
      $connect->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND , "SET character_set_connection =utf8mb4");
      echo "connected Successfully";

    }
    catch (PDOException $error){
    echo ("unable to connect".$error->getMessage());

    }
    ?>

相关问题