归类 "latin1_swedish_ci" 中的数据库回显导致网页上出现问号符号

Echo from database in collation "latin1_swedish_ci" results in questionmark symbols on webpage

我觉得这应该是一个简单的解决方法,但我找不到答案。我试过谷歌搜索,并在这里搜索......无济于事。

我的数据库中存储了一段很长的文本。 排序规则是 latin1_swedish_ci" 当我在数据库中看到它时,它已正确存储。例如:

     string= Sally was walking one day and saw Tom.  Tom said "Hi, Sally!" Sally's response was "Hi, Tom."

每个 " 或 ' 都显示为黑色菱形上的白色问号。

我要

     $result=mysqli_query($db,"SELECT string FROM Table WHERE 1")
     while($row = mysqli_fetch_assoc($result)){
          echo $row['string']; 
     }

并让所有角色都出现。 有人可以帮忙吗?

您可以使用此示例修复您的问题。

SQL:

    CREATE TABLE `test` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `string` longtext,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;

INSERT INTO `test` VALUES ('1', 'Sally was walking one day and saw Tom.  Tom said \"Hi, Sally!\" Sally\'s response was \"Hi, Tom.\"');

PHP:

$query = "SELECT id, string FROM test WHERE id = 1";

if ($result = mysqli_query($link, $query)) {

    while ($row = mysqli_fetch_assoc($result)) {
        echo $row['string'];
    }

    mysqli_free_result($result);
}

mysqli_close($link);

一个问号是一种症状;一串问号是另一个;而带有问号的黑钻又是一个问题。一扫而空...摆脱所有编码功能并...

  • 将客户端中的数据进行utf8编码,并且
  • 建立连接CHARACTER SET utf8
  • 让 tables/columns 为 CHARACTER SET utf8,并且
  • 在 html 页上,使用 <meta charset=UTF-8>

经过进一步研究,我发现了一个以前没有出现过的 post。 Emil H 提供了类似问题的解决方案:

MySQL performs character set conversions on the fly to something called the connection charset. You can specify this charset using the sql statement

SET NAMES utf8 or use a specific API function such as mysql_set_charset():

mysql_set_charset("utf8", $conn); If this is done correctly there's no need to use functions such as utf8_encode() and utf8_decode().

You also have to make sure that the browser uses the same encoding. This is usually done using a simple header:

header('Content-type: text/html;charset=utf-8'); (Note that the charset is called utf-8 in the browser but utf8 in MySQL.)

In most cases the connection charset and web charset are the only things that you need to keep track of, so if it still doesn't work there's probably something else your doing wrong. Try experimenting with it a bit, it usually takes a while to fully understand.

shareedit edited Mar 25 '09 at 12:17 answered Mar 25 '09 at 11:52

Emil H 28k75778

读完之后,我查阅了 php 代码 she/he 正在谈论的内容,发现有一个等效的 sqli,但语法不同。

(PHP 5 >= 5.0.5, PHP 7) mysqli::set_charset -- mysqli_set_charset — 设置默认客户端字符集

描述¶

面向对象风格

bool mysqli::set_charset ( 字符串 $charset ) 程序风格

bool mysqli_set_charset(mysqli $link,字符串 $charset) 设置在从数据库服务器发送数据和向数据库服务器发送数据时使用的默认字符集。

我希望这对遇到我遇到的问题的人有所帮助。