PHP, mysql encoding UTF-8 -
i'm making basic php & mysql searching. our country use character encoding utf-8 or euc-kr .
when input keyword english, result shown well. but, input korean keyword, result doesn't shown on screen. (result count doesn't shown) i'm coding on eclipse pdt, every html,php document's encodings euc-kr. set property. , mysql's table collation euckr_korean. don't know do. i'm newbie on php.
code simple. there 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 data title '%{$keywords}%' "); ?> <div class="result-count"> found <?php echo $query->num_rows; ?> results. </div> <?php } ?>
database table
text | code 삼성전자 | 005930 현대차 | 005380 lg | 003550
when input 'lg (english) ' , result's count 1 (correctly shown)
how solve problem? need help... thanks.
you have not specified charset in search.php
. have not shown <meta charset>
setting in index.html
i'm guessing happening this:
index.html
sends variables (keywords
) in utf-8 / euc-kr.search.php
receives bytestream, , processes utf-8 (problem here).- when
search.php
queries db, using gibberish non-english characters. matches no row in mysql , hence shows no results.
you need keep in mind every php file html file on client-side. in case, search.php
evaluates single-line html file following content:
<div class="result-count">found x results.</div>
notice document missing <!doctype>
, <meta charset>
declarations. html4 browsers typically default iso-8859-1, while html5 browsers default utf-8, therein lies problem.
to solve it, 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>
lastly, consider using utf-8. it's better web.
Comments
Post a Comment