从数据库中选择希伯来语数据时变得乱码

时间:2018-10-13 12:16:09

标签: php mysql phpmyadmin character-encoding hebrew

我在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。 这是给定的结果:׳“׳ ׳™ היי 我想念什么?

1 个答案:

答案 0 :(得分:0)

您要发送包含charset=windows-1255的标头,然后要发送包含charset=utf-8的元标记。这将使调试变得困难。

无论如何,您确定已经正确配置了MySQL以使用UTF-8吗?此处说明了该过程:How to make MySQL handle UTF-8 properly