从数据库中选择希伯来语数据时出现乱码

Getting gibberish when selecting hebrew language data from database

我的 php 项目中的希伯来语有问题。 当我尝试 select 来自 phpmyadmin 数据库的任何希伯来语数据时,它总是 returns 是乱码。 我尝试了包括此处在内的多个站点的一些解决方案,但没有任何帮助。

这是我的简单页面,我想在其中打印来自数据库的简单数据: login.php:

<?php
 header('Content-Type: text/html; charset=windows-1255');
 require_once("dbClass.php");
 require_once("User.php");
?>
<!DOCTYPE html>
<html dir="rtl" lang="he">
<head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    </head>
<body>
<?php
 $db = new dbClass;
    $user = $db->getUser('dany@gmail.com', '123');
    if($user != null){
        echo $user->getFirstName() . " היי";
    }
?>
</body>
</html>

这是我的 User class 我用数据填充对象的地方: User.php:

<?php
 header('Content-Type: text/html; charset=windows-1255');
 class User {
    protected $email;
    protected $firstName;
    protected $lastName;
    protected $password;
    protected $manager;

    public function getEmail(){
        return $this->email;
    }

    public function setEmail($email){
        $this->email = $email;
    }

    public function getFirstName(){
        return $this->firstName;
    }

    public function setFirstName($firstName){
        $this->firstName = $firstName;
    }

    public function getLastName(){
        return $this->lastName;
    }

    public function setLastName($lastName){
        $this->lastName = $lastName;
    }

    public function getPassword(){
        return $this->password;
    }

    public function setPassword($password){
        $this->password = $password;
    }

    public function getManager(){
        return $this->manager;
    }

    public function setManager($manager){
        $this->manager = $manager;
    }
?>

这是我的 database class 所有查询和数据库连接的地方 dbClass.php:

<?php
 header('Content-Type: text/html; charset=windows-1255');
 require_once("User.php");

class dbClass{

    private $host;
    private $db;
    private $charset;
    private $user;
    private $pass;
    private $opt = array(

    PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE=>PDO::FETCH_ASSOC);

    private $connection;

    public function __construct(string $host="localhost",string $db="mydatabase", string $charset= "utf8", string $user = "root", string $pass="")
    {
        $this->host=$host;
        $this->db=$db;
        $this->charset=$charset;
        $this->user=$user;
        $this->pass=$pass;      
    }

    private function connect(){

        $dsn = "mysql:host=$this->host;dbname=$this->db;charset=$this->charset";
        $this->connection = new PDO($dsn,$this->user,$this->pass,$this->opt);
        $this->connection->exec("SET NAMES utf8");

    }

    public function disconnect(){

        $this->connection = null;
    }   

    public function getUser(string $email, string $password){
        $this->connect();
        $statment = $this->connection->prepare("SELECT * FROM users WHERE email=:email AND password=:password");
        $statment->execute([':email'=>$email, ':password'=>$password]);
        $userArray = array();
        /*while($row=$statment->fetchObject('User'))    // another option - also not works
            $userArray[] = $row;*/
        while($row=$statment->fetch(PDO::FETCH_ASSOC))  {
            $user = new User;
            $user->setEmail($row['email']);
            $user->setFirstName($row['firstName']);
            $userArray[] = $user;
        }           
        $this->disconnect();
        if(isset($userArray[0]))
            return $userArray[0];
        else
            return null;
    }
?>

我在 phpmyadmin 中的所有表都设置为 utf-8。 这是给定的结果:׳“׳ ׳™ היי 我错过了什么?

您发送了一个包含 charset=windows-1255 的 header,但随后您发送了一个包含 charset=utf-8 的元标记。这将使调试变得困难。

无论如何,您确定已正确配置 MySQL 以使用 UTF-8 吗?该过程在这里解释:How to make MySQL handle UTF-8 properly