Получение тарабарщины при выборе данных иврита из базы данных - PullRequest
0 голосов
/ 13 октября 2018

У меня проблема с ивритом в моем проекте 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 голосов
/ 14 октября 2018

Вы отправляете заголовок, содержащий charset=windows-1255, но затем отправляете метатег, содержащий charset=utf-8.Это затруднит отладку.

В любом случае, вы уверены, что правильно настроили MySQL для использования UTF-8?Процесс объясняется здесь: Как заставить MySQL правильно обрабатывать UTF-8

...