PHP,mysql编码UTF-8 [英] PHP, mysql encoding UTF-8

查看:88
本文介绍了PHP,mysql编码UTF-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作基本的PHP&MySQL搜索.我国通常使用utf-8或euc-kr字符编码.

I'm making basic PHP & MySQL searching. Our country usually use character encoding which is utf-8 or euc-kr .

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

when I input the keyword that is English, the result is shown well. but, input the Korean keyword, the result doesn't shown on the screen. (result count doesn't shown) I'm coding on Eclipse PDT, every html,php document's encodings are EUC-KR. I set up property. and MySQL's table collation is euckr_korean. I don't know what to do. I'm newbie on php.

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

Code is simple. there are 2 document.

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(正确显示)

when I input 'LG (English) ' , the result's count is 1 (correctly shown)

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

How to solve this problem? I need your help... Thanks.

推荐答案

您尚未在 search.php 中指定字符集.您还没有在 index.html

You have not specified your charset in search.php. You have also not shown us the <meta charset> setting in your index.html

我猜这可能是怎么回事:

I'm guessing what is probably happening is this:

  1. index.html 使用UTF-8/EUC-KR发送GET变量(关键字).
  2. search.php 接收一个字节流,并将其作为UTF-8处理(这里有问题).
  3. search.php 查询数据库时,它在使用非英语字符的乱码.它在MySQL中不匹配任何行,因此不显示任何结果.
  1. index.html sends GET variables (keywords) in UTF-8 / EUC-KR.
  2. search.phpreceives a bytestream, and processes it as UTF-8 (problem here).
  3. When search.php queries the DB, it is using some gibberish for non-English characters. It matches no row in MySQL and hence shows no results.

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

You need to keep in mind that every PHP file is actually ultimately an HTML file on the client-side. In your case, your search.php evaluates to a single-line HTML file with the following content:

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

请注意,此文档缺少<!DOCTYPE> < meta charset> 声明.HTML4浏览器通常默认使用ISO-8859-1,而HTML5浏览器默认使用UTF-8,因此存在问题.

Notice that this document is missing the <!DOCTYPE> and <meta charset> declarations. HTML4 browsers typically default to ISO-8859-1, while HTML5 browsers default to UTF-8, so therein lies the problem.

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

To solve it, always start every HTML page with:

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

最后,强烈考虑使用UTF-8.对于网络来说更好.

Lastly, strongly consider using UTF-8. It's better for the web.

这篇关于PHP,mysql编码UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆