0

I have a problem with hebrew language on my php project. When I'm trying to select any hebrew data from phpmyadmin database it always returns as a gibberish. I tried a few solutions from multiple sites including here but nothing helped me.

This is my simple page where I want to print a simple data from the database: 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>

This is my User class where I fill the object with data: 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;
    }
?>

This is my database class where all the queries and connection to 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;
    }
?>

All my tables in phpmyadmin are set to utf-8. this is the given result: ׳“׳ ׳™ היי What am I missing?

Bobby Axe
  • 1,491
  • 13
  • 29
Black-Wolf
  • 59
  • 9

1 Answers1

0

You're sending a header containing charset=windows-1255 but then you're sending a meta tag containing charset=utf-8. That is going to make things difficult to debug.

In any case, are you sure you've properly configured MySQL to use UTF-8? The process is explained here: How to make MySQL handle UTF-8 properly

kmoser
  • 8,780
  • 3
  • 24
  • 40