简体   繁体   English

使用PHP 5.5 UTF-8问题将更新插入MYSQL

[英]Insert Update to MYSQL with PHP 5.5 UTF-8 issues

I can't understand whats problem. 我无法理解什么是问题。 I write data to mysql with php and utf-8 issues 我用php和utf-8问题将数据写入mysql

Write in HTML form Binəqədi r. 用HTML形式写Binəqədir。 Çiçək qəs Çiçəkqəs

Writed database Bin?q?di r. Writed数据库Bin?q?di r。 ÃÆ'ƒÆ’ÃÆ' ÃÆ'Æ•••â€

When I use mysqli_set_charset($conn,"utf8"); 当我使用mysqli_set_charset($conn,"utf8"); in insert and update query 插入更新查询中

Writed database Bin?q?di r. Writed数据库Bin?q?di r。 Çiç?kq?s 中投?KQ?的

Write data manually direct to database and use mysqli_set_charset($conn,"utf8"); 手动将数据直接写入数据库并使用mysqli_set_charset($conn,"utf8"); in select works normal. 选择工作正常。

How can I fix it? 我该如何解决? INSERT and UPDATE with UTF-8. 使用UTF-8进行INSERTUPDATE This is my insert code 这是我的插入代码

public function insert($sql) {
    $conn = new mysqli(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    mysqli_set_charset($conn,"utf8");
//  $conn->set_charset("utf8"); use same this.
    if ($conn->query($sql) === TRUE) {
        return 1;
    } else {
        return 0;
    }
    $conn->close();
}

NOTE: This issues on Linux and OSX. 注意:此问题在Linux和OSX上。 In windows work goods. 在windows工作的商品。 How can I fix it for linux hosting? 我如何修复它为Linux托管?

try 尝试

AddDefaultCharset utf-8

in you apache configuration 在你的apache配置中

or in you php file use 或者在你的php文件中使用

header( 'content-type: text/html; charset=utf-8' );

and also try to 并尝试

default_charset = "utf-8";

in you php.ini 在你的php.ini

also you can try with 你也可以试试

  [client]
  default-character-set=utf8

  [mysql]
  default-character-set=utf8

  [mysqld]
  character-set-client-handshake = false #force encoding to uft8
  character-set-server=utf8
  collation-server=utf8_general_ci

  [mysqld_safe]
  default-character-set=utf8

in your my.ini and restart mysql demon 在你的my.ini中重新启动mysql恶魔

Please consider all of these: 请考虑所有这些:

  1. Before your queries, do this: 在查询之前,请执行以下操作:

     $conn->query('SET NAMES utf8'); 
  2. Be sure your file encoding is utf8 (not the html encoding). 确保您的文件编码是utf8(不是html编码)。 I mean the file encoding which your IDE is using. 我指的是IDE正在使用的文件编码。

  3. Be sure about your table charset 确保你的桌子字符集

This is clearly a duplicate of a well-known issue , but cannot be flagged for closure due to the bounty. 这显然是一个众所周知的问题的重复,但由于赏金,不能标记为关闭。 So, the checklist: 所以,清单:

  1. Ensure database columns, tables, and database are set to character set utf8mb4 确保将数据库列,表和数据库设置为字符集utf8mb4
  2. Ensure the connection is set to the same character set with $conn->set_charset("utf8mb4"); 确保使用$conn->set_charset("utf8mb4");将连接设置为相同的字符集$conn->set_charset("utf8mb4");
  3. Ensure your server is configured to inform web browsers that it will be sending Unicode text. 确保您的服务器配置为通知Web浏览器它将发送Unicode文本。 In PHP, do header("Content-Type: text/html;charset=utf8"); 在PHP中,执行header("Content-Type: text/html;charset=utf8"); or set it up in your web server configuration. 或者在Web服务器配置中进行设置。

Try using set names 尝试使用集名称

public function insert($sql) {
    $conn = new mysqli(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
   // $conn->set_charset("utf8");
    $conn->query("SET NAMES 'utf8'");
    if ($conn->query($sql) === TRUE) {
        return 1;
    } else {
        return 0;
    }
    $conn->close();
}

From MySQL ( http://dev.mysql.com/doc/refman/5.7/en/charset-connection.html ): 来自MySQL( http://dev.mysql.com/doc/refman/5.7/en/charset-connection.html ):

  • SET NAMES 'charset_name' [COLLATE 'collation_name'] SET NAMES'charset_name'[COLLATE'collat​​ion_name']

SET NAMES indicates what character set the client will use to send SQL statements to the server. SET NAMES指示客户端将用于将SQL语句发送到服务器的字符集。 Thus, SET NAMES 'cp1251' tells the server, “future incoming messages from this client are in character set cp1251.” It also specifies the character set that the server should use for sending results back to the client. 因此, SET NAMES 'cp1251'告诉服务器,“来自此客户端的未来传入消息是字符集cp1251。”它还指定了服务器用于将结果发送回客户端的字符集。 (For example, it indicates what character set to use for column values if you use a SELECT statement.) (例如,如果使用SELECT语句,它指示要用于列值的字符集。)

A SET NAMES 'charset_name' statement is equivalent to these three statements: SET NAMES 'charset_name'语句等同于这三个语句:

SET character_set_client = charset_name;
SET character_set_results = charset_name;
SET character_set_connection = charset_name;

Setting character_set_connection to charset_name also implicitly sets collation_connection to the default collation for charset_name. 将character_set_connection设置为charset_name也会将collat​​ion_connection隐式设置为charset_name的默认排序规则。 It is unnecessary to set that collation explicitly. 没有必要明确设置该排序规则。 To specify a particular collation, use the optional COLLATE clause: 要指定特定的排序规则,请使用可选的COLLATE子句:

  • SET CHARACTER SET charset_name SET CHARACTER SET charset_name

SET CHARACTER SET is similar to SET NAMES but sets character_set_connection and collation_connection to character_set_database and collation_database. SET CHARACTER SET类似于SET NAMES但将character_set_connection和collat​​ion_connection设置为character_set_database和collat​​ion_database。 A SET CHARACTER SET charset_name statement is equivalent to these three statements: SET CHARACTER SET charset_name语句相当于这三个语句:

SET character_set_client = charset_name;
SET character_set_results = charset_name;
SET collation_connection = @@collation_database;

Setting collation_connection also implicitly sets character_set_connection to the character set associated with the collation (equivalent to executing SET character_set_connection = @@character_set_database). 设置collat​​ion_connection还隐式将character_set_connection设置为与排序规则关联的字符集(相当于执行SET character_set_connection = @@ character_set_database)。 It is unnecessary to set character_set_connection explicitly. 没有必要显式设置character_set_connection。

My opinion is that in some point the proper character set for the field description is not specified when using stored procedure. 我的观点是,在某些时候,使用存储过程时没有指定字段描述的正确字符集。 Do check again that your database/table/field have as character set utf8_general_ci 再次检查您的数据库/表/字段是否具有字符集utf8_general_ci

From MySQL ( http://dev.mysql.com/doc/refman/5.7/en/charset-connection.html ): 来自MySQL( http://dev.mysql.com/doc/refman/5.7/en/charset-connection.html ):

Example: Suppose that column1 is defined as CHAR(5) CHARACTER SET latin2 . 示例:假设column1定义为CHAR(5) CHARACTER SET latin2 If you do not say SET NAMES or SET CHARACTER SET , then for SELECT column1 FROM t , the server sends back all the values for column1 using the character set that the client specified when it connected. 如果您没有说SET NAMESSET CHARACTER SET ,那么对于SELECT column1 FROM t ,服务器使用客户端连接时指定的字符集发回column1所有值。 On the other hand, if you say SET NAMES 'latin1' or SET CHARACTER SET latin1 before issuing the SELECT statement, the server converts the latin2 values to latin1 just before sending results back. 另一方面,如果在发出SELECT语句之前说SET NAMES 'latin1'SET CHARACTER SET latin1 ,服务器会在发送结果之前将latin2值转换为latin1。 Conversion may be lossy if there are characters that are not in both character sets. 如果两个字符集中都没有字符,则转换可能会有损。

The gibberish you displayed seems to involve more than one error. 您显示的乱码似乎涉及多个错误。 Çiç , when Mojibaked to latin1 gives Çiç . Çiç ,当Çiç到latin1给Çiç

Please provide SELECT col, HEX(col) FROM ... for something that is 'bad'. 请提供SELECT col, HEX(col) FROM ...用于“坏”的内容。

Meanwhile, your reply about the stored procedure -- Do SHOW CREATE PROCEDURE ... and check what character set was in effect when the procedure was created. 同时,您对存储过程的回复 - 执行SHOW CREATE PROCEDURE ...并检查SHOW CREATE PROCEDURE ...时有效的character set That could be part of the problem. 可能是问题的一部分。 (I noticed that that decade-old bug report you mentioned failed to consider this aspect.) (我注意到你提到的这个十年前的错误报告没有考虑到这方面。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM