doushang2571 2012-09-12 21:17
浏览 42
已采纳

在MySQL中存储特殊字符(Android,PHP也涉及)

Ok, I am trying to store data in a MySQL database from an Android app. I use PHP as the go between.

What I submit to the database is this (for example): Sakè

What is stored in MySQL is: Sakè

When I print the line back out in Android it knows to convert back to: Sakè

The problem is, when I do certain select statements, yeah it basically doesn't work out like it should. Is there any way I can keep this consistent?

Just a note in MySQL: I use utf8_general_ci

  • 写回答

1条回答 默认 最新

  • dongnaizao8039 2012-09-12 21:20
    关注

    You need to enforce correct character for your database connection for your PHP scripts. Simplest approach would be to execute this query:

    SET NAMES 'utf8';
    

    just once, best after you connect(). If you use Zend Framework, just add this:

    resources.db.params.charset  = UTF8
    

    to your application.ini. For mysqli see set_charset(). If you use anything else, check the docs for something similar. If there's nothing, the SET NAMES trick would always work.

    One important note though - depending on how you put your data to the DB, your database content most likely require fixing as it is corrupted by broken charset during INSERTs. So if even you do SET NAMES now, you most likely still see old data shown wrong. But that is, ironically, OK - you are now showing all fine, but data itself is broken. You just now need to fix your data (just re-insert it).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图