PHP、mysql编码UTF-8

标签 php mysql encoding utf-8

我正在进行基本的 PHP 和 MySQL 搜索。 我国通常使用的字符编码是 utf-8 或 euc-kr 。

当我输入英文关键字时,结果显示良好。 但是,输入韩语关键字,结果并没有显示在屏幕上。 (未显示结果计数) 我在 Eclipse PDT 上编码, 每个 html、php 文档的编码都是 EUC-KR。我设置属性。 MySQL 的表排序规则是 euckr_korean。 我不知道该怎么办。我是 php 的新手。

代码很简单。有2个文件。

index.html

<body>
    <form action="./search.php" method="get">
    <label> 
        Search
        <input type="text" name="keywords"> 
        <input type="submit" value="Search">
    </label>
    </form>
</body>

search.php

<?php



$db = new mysqli('localhost', 'root', 'apmsetup', 'consen');

if(isset($_GET['keywords'])){

$keywords = $db->escape_string($_GET['keywords']);

$query = $db->query("
        SELECT title
        FROM data
        WHERE title LIKE '%{$keywords}%' 
        ");
?>


<div class="result-count">
    Found <?php echo $query->num_rows; ?> results. </div>


<?php 
}

?>

数据库表

text         |  code

삼성전자      | 005930
현대차        |    005380
LG           |  003550

当我输入 'LG (English) ' 时,结果的计数为 1(正确显示)

如何解决这个问题?我需要你的帮助...谢谢。

enter image description here

最佳答案

您没有在 search.php 中指定您的字符集.您也没有向我们展示 <meta charset>在您的 index.html 中设置

我猜可能是这样的:

  1. index.html以 UTF-8/EUC-KR 格式发送 GET 变量 ( keywords )。
  2. search.php接收一个字节流,并将其处理为 UTF-8(这里有问题)。
  3. 何时search.php查询数据库,它对非英语字符使用了一些乱码。它不匹配 MySQL 中的任何行,因此不显示任何结果。

您需要记住,每个 PHP 文件实际上最终都是客户端的 HTML 文件。在你的情况下,你的 search.php评估为具有以下内容的单行 HTML 文件:

<div class="result-count">Found X results.</div>

请注意,此文档缺少 <!DOCTYPE><meta charset>声明。 HTML4 浏览器通常默认为 ISO-8859-1,而 HTML5 浏览器默认为 UTF-8,所以这就是问题所在。

要解决这个问题,请始终以以下内容开始每个 HTML 页面:

<!DOCTYPE html>
<html lang="en"> <!-- or whatever language code -->
   <head>
       <meta charset="utf-8"> <!-- or whatever charset -->
       <title>...</title>
       ...
   </head>

最后,强烈考虑使用 UTF-8。它更适合网络。

关于PHP、mysql编码UTF-8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30814423/

相关文章:

php - 基本 mysql/数据存储问题 : information architecture, 代码本身不多

php - Mysql中存储的特殊字符

php - 如何使用命令提示符 : error 连接到 mysql 服务器

javascript - decodeURIComponent vs unescape,unescape有什么问题?

algorithm - 放气算法伪代码

php - 比较日期修整

php - 强制下载 CSV 文件

php - 如何从 html 插入 select/option 并从 SQL 表中的 array_rand 插入变量

php - 如何独立于 'linked' 服务器添加对 phpMyAdmin 的用户名/密码访问?

xml - Go xml.Marshal 返回无效字符