doupeng5320 2015-02-15 15:09
浏览 60
已采纳

PDO将错误的日文字符插入数据库

Background


I'm currently writing a small web application for myself to test my Japanese. It consists of getting to see a Japanese word, and then giving the correct translation in Dutch. I have a small set-up where I can specify a Japanese word with the Dutch translation, and it will get inserted in the database.

Problem


When inserting certain Japanese characters, such as "お" (or "O" for those wondering) it gets inserted in the database as "ず" (or "Zu"). There are more characters which also get transformed, but I forgot which (I cleared the database to see if certain solutions worked).

What have I tried


I've made sure that all the things I think need to be UTF-8 are set to UTF-8. I've set the following to UTF-8:
  • HTML page header:
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  • PHP header (very first line):
    header('Content-Type: text/html; charset=utf-8');
  • The MySQL table japanese_words:
    ALTER TABLE japanese_words CONVERT TO CHARACTER SET utf8;
  • PDO encoding:
    $db = new PDO('mysql:host=localhost;dbname=japanese;charset=utf8', '****', '****');
    And tried:
    $db->exec("set names utf8");

Specifying the Japanese word works in PHP. I can dump, and regularly show, the Japanese words in my webpage. This means my HTML and PHP are set up correctly. So there are no errors before inserting it in the database.

When I insert the Japanese words manually in the database it works.

The characters get "transformed" when I try to insert it in to the database using PDO.

Trying to show the query PDO generates doesn't work. I get the query with the bound parameter and not the actual word.

Code


add_word.php (this all works fine):
$japanese_word = $_POST['japanese_word'];
$dutch_word = $_POST['dutch_word'];

$word = new Word();
$word->setDutchWord($dutch_word);
$word->setJapaneseWord($japanese_word);

$wordDAO = new WordDAO($db);
$success = $wordDAO->addWord($word);

WordDAO method, where I think the problem is (or somewhere between there and the actual MySQL database):

$query = "insert into `japanese_words` (`word`) values(:word)";
$stmt = $this->db->prepare($query);
$stmt->bindParam(':word', strtolower($word->getJapaneseWord()));
$stmt->execute();
$japanese_word_id = $this->db->lastInsertId();

Question


What am I doing wrong here? Did I forget to set up more encodings, or specify a particular PDO setting? Any help is appreciated.
  • 写回答

2条回答 默认 最新

  • dongmu6225 2015-02-15 15:14
    关注

    The problem is I was using strtolower() in my bindParam. This changed the values of the Japanese words. Changing the code to the following worked:

    $query = "insert into `japanese_words` (`word`) values(:word)";
    $stmt = $this->db->prepare($query);
    $stmt->bindParam(':word', $word->getJapaneseWord());
    $stmt->execute();
    $japanese_word_id = $this->db->lastInsertId();
    

    Damn my blatant copy/pasting (of course I found out as soon as I posted the question).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效