douchuxun4162 2012-09-08 01:30
浏览 51
已采纳

编码地狱。 Mysql,php和utf-8

Performing a query comparing phrases is a pain in the ass. Look the following configuration to understand my problem.


sql.sql

create user "myStore"@"localhost" identified by "1234"; grant all privileges on myStore.* TO "myStore"@"localhost";

create database myStore character set utf8 collate utf8_general_ci;

use myStore;

create table item (
    item_id integer not null,
    name varchar(60) not null,

    primary key (item_id)
);

alter table item engine=innodb default charset=utf8 collate=utf8_bin;

insert into item (item_id, name) values (0, "apple"), (1, "maça");

my.cnf

[mysqld]
collation-server = utf8_unicode_ci
character-set-server = utf8

mysql> SHOW VARIABLES LIKE 'collation%';

+----------------------+-----------------+
| Variable_name        | Value           |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database   | utf8_unicode_ci |
| collation_server     | utf8_unicode_ci |
+----------------------+-----------------+

mysql> SHOW VARIABLES LIKE 'character_set%';

+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+

index.php

<?php
    try {
        header ('Content-Type: text/html; charset=utf-8'); 

        $mysqli = new mysqli ("localhost", "myStore", "1234", "myStore");

        if ($mysqli->connect_error) throw new exception ("Error (" . $mysqli->connect_errno . "). " . $mysqli->connect_error);

        $sql = $mysqli->prepare ("select item_id, name from item where name = '" . utf8_decode ("maça") ."'");

        if (! $sql) throw new exception ($mysqli->error);

        $sql->execute ();

        $sql->bind_result ($item_id, $name); 

        while ($sql->fetch ()) echo $item_id . " - " . utf8_encode ($name);

        $sql->close ();
    }
    catch (exception $e) {
        echo $e;
    }
?>

As you can see, I MUST decode the string before sending it to MySQL and then encode it again.

I really don't know what is wrong, is there a way to avoid those encoding and decoding functions ? Thanks.

  • 写回答

1条回答 默认 最新

  • dre75230 2012-09-08 01:41
    关注

    mysql> SHOW VARIABLES LIKE 'character_set%';

    +--------------------------+----------------------------+
    | Variable_name            | Value                      |
    +--------------------------+----------------------------+
    | character_set_client     | utf8                       |
    | character_set_connection | utf8                       |
    

    These variables are session-specific. Please ensure that they are set appropriately for your client during your sessions by calling SET NAMES 'charset_name' during an interactive mysql session, or mysqli::set_charset ( string $charset ) from PHP.

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

报告相同问题?

悬赏问题

  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证