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

yshpjwxd  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(416)

我在php项目中遇到希伯来语问题。当我试图从phpmyadmin数据库中选择任何希伯来语数据时,它总是以乱七八糟的方式返回。我尝试了几个解决方案,从多个网站,包括在这里,但没有帮助我。
这是我的简单页面,我想从数据库中打印一个简单的数据: 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 用数据填充对象的类: 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 类,其中包含所有查询和到数据库的连接 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。这是给定的结果: ׳“׳ ׳™ היי 我错过了什么?

egdjgwm8

egdjgwm81#

你正在发送一个包含 charset=windows-1255 但是你要发送一个包含 charset=utf-8 . 这将使调试变得困难。
在任何情况下,你确定你已经正确配置mysql使用utf-8了吗?这里解释了这个过程:如何让mysql正确地处理utf-8

相关问题