-2
<?php 
    $path               = "mysql:host=localhost;dbname=kanjisearch;charset=utf-8";
    $pdo                = new PDO($path, "xxx", "xxx");
    $sth                = $pdo->prepare("SELECT * FROM `kanji` WHERE `kanjiName` = '$kanji'");
    $sth->execute();
    $results            = $sth->fetchAll();
?>

I am building a PHP search that uses Chinese characters in the actual SQL query, I have -> exec("SET CHARACTER SET utf8"); on the pdo. And it renders just fine, the text I bring back renders the charects great, if I put numbers ins the sql but I need to search based on the Kanji, so when I query with WHERE kanji = 一, the 一 doesn't seem to go through ok, it renders on the screen on the debug, but just wont query, results return empty but when I copy the query directly into PHPMyAdmin it returns the result.

user3238825
  • 1
  • 1
  • 3
  • SELECT * FROM `kanji` WHERE `kanjiName` = '一' In the in PHPMyAdmin works just fine. However this does not, SELECT * FROM `kanji` WHERE `kanjiName` = '$kanji' and its the same thing – user3238825 Jan 27 '14 at 01:29
  • 1
    Probably you need `SET NAMES utf8` [as described in PDO manual](http://cz1.php.net/manual/en/ref.pdo-mysql.connection.php). Before PHP 5.3.6 the `charset` parameter in DNS was ignored. Well, now I noticed you specify `utf-8`. Try changing it to `utf8` (remove the hyphen). MySQL does not like the hyphenated form. – Palec Jan 27 '14 at 02:01
  • What is the definition of `kanji.kanjiName` column? What is the charset and collation of the `kanji` table? Why don’t you prepare the query with `WHERE kanjiName = ?`? Then you would supply `$kanji` in `$sth->ececute($kanji)`. Is `$kanji` really encoded in UTF-8? – Palec Jan 27 '14 at 05:20

3 Answers3

0

Were your tables and or columns created with the UTF-8 character set as the default encoding? By default MySQL creates tables with a latin based charset and collate options. UTF-8, especially none latiin characters, gets mangled when it is saved in a latin encoded column.

Try changing the table's charset to use UTF8 using the ALTER table command and reloading/inserting your data.

joatis
  • 3,375
  • 1
  • 17
  • 13
-1

Try add encoding (accept-charset="UTF-8" and charset=utf-8") to your form and html head:

<form method="post" action="yourAction" accept-charset="UTF-8">

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

EDIT: also have a look at this similar question here. (by shark555)

$pdo = new PDO( 
    'mysql:host=hostname;dbname=defaultDbName', 
    'username', 
    'password', 
    array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8") 
); 
Community
  • 1
  • 1
jonasnas
  • 3,540
  • 1
  • 23
  • 32
  • I know this part, this part is easy that just parsing it to the page. But once on the search in need PHP to send the SQL query – user3238825 Jan 27 '14 at 01:24
  • This is not the same as my question at all, he is asking about rendering, mine renders just fine. It just does not query with these chars. – user3238825 Jan 27 '14 at 01:41
  • Sorry for doubting you dude, I read the article more and yes this was the right thing, first time I just copied and pasted it. Got to learn to read more, Thank you! – user3238825 Jan 27 '14 at 04:36
-1

$kanji needs to be handled as a particular/specific character set. so, what's typed in is likely not seen as a UTF-8 encoded string. you need to handle this.

note: http://www.fileformat.info/info/unicode/char/4e00/index.htm

basically you want to convert to UTF-8. but try this: http://us1.php.net/mb_convert_encoding

like

  • mb_convert_encoding($text, 'UTF-8');
  • or if you know the encoding of the language stick as the 3rd param.
  • or try auto: mb_convert_encoding($text, 'UTF-8', 'auto');
Rob
  • 214
  • 1
  • 5