gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 16:51:31 24 4
gpt4 key购买 nike

我的 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。这是给定的结果:׳“׳ ׳™ הй我错过了什么?

最佳答案

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

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

关于php - 从数据库中选择希伯来语数据时出现乱码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52792817/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com