gpt4 book ai didi

php - 编码为希伯来语 - PHP, mysqli

转载 作者:行者123 更新时间:2023-11-29 04:22:48 25 4
gpt4 key购买 nike

我正在使用 MySQLi 从数据库中检索信息。我所有的页面都是用没有 BOM 的 UTF-8 编码的,我有 2 个页面要处理,但主要问题是在数据库上,信息以常规方式显示——但是当我检索它时,它是这样的:

������ ���� - ����?

在 phpMyAdmin 面板上 - 列是 latin1_general_ci(我也尝试将其设置为 utf8_general - 相同的输出!)。

“mysql.php”:

<?php
header('Content-type: text/xhtml; charset=windows-1255');
$mysql = new mysqli('localhost', 'elenbyin_vadim', 'pass33323', 'elenbyin_vadim');
?>

“index.php”,主要部分:

<?php
include ('mysql.php');
include('functions.php');

if($_GET["lang"] == 'rus')
$lang = 'rus';
else if($_GET["lang"] == 'heb')
$lang = 'heb';
else
$lang = 'heb';

……

<div class="main_text">
<?

$result = $mysql->query('SELECT * FROM homepage');
while($row = mysqli_fetch_array($result)){
echo "".$row['title']."<br />".$row['text_heb']."";
}
?>
</div>

有人可以帮我解决这个问题吗?

最佳答案

您必须更改字符集才能使其工作:

<?php
$mysql->set_charset('utf8');
$result = $mysql->query('SELECT * FROM homepage');
...
?>

@Class 的评论 - 如果您在所有页面中都需要它,您可以将它添加到 mysql.php:

<?php
header('Content-type: text/xhtml; charset=windows-1255');
$mysql = new mysqli('localhost', 'elenbyin_vadim', 'pass33323', 'elenbyin_vadim');
$mysql->set_charset('utf8');
?>

此外,请确保将排序规则设置为 utf8_general_ci

一种较旧的方法是将名称设置为 utf8:

<?php
$mysql->query("SET NAMES 'utf8'");
$result = $mysql->query('SELECT * FROM homepage');
...
?>

但是根据set_charset() docs第一个是可取的:

Note: This is the preferred way to change the charset. Using mysqli_query() to set it (such as SET NAMES utf8) is not recommended. See the MySQL character set concepts section for more information.

关于php - 编码为希伯来语 - PHP, mysqli,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18950307/

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