PHP, mysql UTF-8 encoding

I am doing basic PHP and MySQL search. In our country, the character encoding is usually used, which is utf-8 or euc-kr.

when I enter a keyword which is English, the result is shown well. but enter keyword Korean, the result is not displayed on the screen. (number of results not shown) I am coding Eclipse PDT, each html, php are EUC-KR document encodings. I have created a property. and the MySQL table mapping is euckr_korean. I do not know what to do. I am new to php.

The code is simple. there are 2 documents.

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 
}

?>

      

Database table

text         |  code

μ‚Όμ„±μ „μž      | 005930
ν˜„λŒ€μ°¨        |    005380
LG           |  003550

      

when I enter "LG (English)" the result is 1 (correctly shown)

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

enter image description here

+3


source to share


2 answers


You have not specified your encoding in search.php

. You also didn't show us the setting <meta charset>

inindex.html

I guess what is probably going on:

  • index.html

    sends GET ( keywords

    ) variables to UTF-8 / EUC-KR.
  • search.php

    receives a byte stream and treats it as UTF-8 (problem here).
  • When search.php

    querying the DB, it uses some gibberish for non-English characters. It does not match a string in MySQL and therefore does not show any results.

You need to keep in mind that every PHP file is actually a client side HTML file. In your case, yours search.php

is evaluating a one-line HTML file with the following content:

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

      



Please note that this document contains no ads <!DOCTYPE>

and <meta charset>

. HTML4 browsers generally default to ISO-8859-1, and HTML5 browsers default to UTF-8, so that's the problem.

To fix this problem, 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>

      

Finally, we strongly recommend using UTF-8. It's better for the internet.

+2


source


I finally got the clue, it's a problem with the default mysql character set. To add "binary ()" to search.php the result was shown well.

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

      

To avoid the same problem later, you need to set the character set value for mysql, (my.cnf on linux, my.ini on window). You can check your default character set in mysql console at

mysql > show variables like '%char%';

      

First, you need to set the encoding to a different one by editing the documnet my.cnf or my.ini file in mysql.



Secondly, import in your php document which

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />"

      

Thirdly, import to html document which

<meta charset="utf-8">

      

0


source







All Articles