简体   繁体   English

MySQL和PHP:带有西里尔字符的UTF-8

[英]MySQL and PHP: UTF-8 with Cyrillic characters

I'm trying to insert a Cyrillic value in the MySQL table, but there is a problem with encoding. 我正在尝试在MySQL表中插入Cyrillic值,但编码存在问题。

Php: PHP的:

<?php

$servername = "localhost";
$username = "a";
$password = "b";
$dbname = "c";

$conn = new mysqli($servername, $username, $password, $dbname);

mysql_query("SET NAMES 'utf8';"); 
mysql_query("SET CHARACTER SET 'utf8';"); 
mysql_query("SET SESSION collation_connection = 'utf8_general_ci';"); 

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "UPDATE  `c`.`mainp` SET  `search` =  'test тест' WHERE  `mainp`.`id` =1;";

if ($conn->query($sql) === TRUE) {   
}
$conn->close();

?>

MySQL: MySQL的:

| id |    search   |            
| 1  |   test ав |

Note: PHP file is utf-8 , database collation utf8_general_ci 注意:PHP文件是utf-8 ,数据库排序规则是utf8_general_ci

You are mixing APIs here, mysql_* and mysqli_* doesn't mix. 你在这里混合API, mysql_*mysqli_*不混合。 You should stick with mysqli_ (as it seems you are anyway), as mysql_* functions are deprecated, and removed entirely in PHP7. 你应该坚持使用mysqli_ (因为看起来你似乎是这样),因为不推荐使用mysql_*函数,并且完全在PHP7中删除。

Your actual issue is a charset problem somewhere. 你的实际问题是某个地方的字符集问题。 Here's a few pointers which can help you get the right charset for your application. 这里有一些指示可以帮助您为您的应用程序获得正确的字符集。 This covers most of the general problems one can face when developing a PHP/MySQL application. 这涵盖了开发PHP / MySQL应用程序时可能遇到的大多数常见问题。

  • ALL attributes throughout your application must be set to UTF-8 应用程序中的所有属性必须设置为UTF-8
  • Save the document as UTF-8 w/o BOM (If you're using Notepad++, it's Format -> Convert to UTF-8 w/o BOM ) 将文档保存为UTF-8(无BOM)(如果您使用的是Notepad ++, Format - > Convert to UTF-8 w/o BOM
  • The header in both PHP and HTML should be set to UTF-8 PHP和HTML中的标题应设置为UTF-8

    • HTML (inside <head></head> tags): HTML (在<head></head>标签内):

       <meta charset="UTF-8"> 
    • PHP (at the top of your file, before any output): PHP (在文件的顶部,在任何输出之前):

       header('Content-Type: text/html; charset=utf-8'); 
  • Upon connecting to the database, set the charset to UTF-8 for your connection-object, like this (directly after connecting) 连接到数据库后,将charset设置为您的connection-object的UTF-8,如下所示(直接连接后)

     mysqli_set_charset($conn, "utf8"); /* Procedural approach */ $conn->set_charset("utf8"); /* Object-oriented approach */ 

    This is for mysqli_* , there are similar ones for mysql_* and PDO (see bottom of this answer). 这是针对mysqli_*mysql_*和PDO也有类似的mysql_* (参见本答案的底部)。

  • Also make sure your database and tables are set to UTF-8, you can do that like this: 还要确保您的数据库和表设置为UTF-8,您可以这样做:

     ALTER DATABASE databasename CHARACTER SET utf8 COLLATE utf8_unicode_ci; ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci; 

    (Any data already stored won't be converted to the proper charset, so you'll need to do this with a clean database, or update the data after doing this if there are broken characters). (已存储的任何数据都不会转换为正确的字符集,因此您需要使用干净的数据库执行此操作,或者在执行此操作后更新数据(如果存在损坏的字符))。

  • If you're using json_encode() , you might need to apply the JSON_UNESCAPED_UNICODE flag, otherwise it will convert special characters to their hexadecimal equivalent. 如果您正在使用json_encode() ,则可能需要应用JSON_UNESCAPED_UNICODE标志,否则它会将特殊字符转换为其十六进制等效项。

Remember that EVERYTHING in your entire pipeline of code needs to be set to UFT-8, otherwise you might experience broken characters in your application. 请记住, 一切都在代码的整个管线需要被设置为UFT-8,否则,你可能在你的应用体验破字。

In addition to this list, there may be functions that has a specific parameter for specifying a charset. 除了此列表之外,可能还有一些函数具有用于指定字符集的特定参数。 The manual will tell you about this (an example is htmlspecialchars() ). 手册会告诉你这个(例子是htmlspecialchars() )。

There are also special functions for multibyte characters, example: strtolower() won't lower multibyte characters, for that you'll have to use mb_strtolower() , see this live demo . 还有多字节字符的特殊功能,例如: strtolower()不会降低多字节字符,因为您必须使用mb_strtolower() ,请参阅此实时演示

Note 1 : Notice that its someplace noted as utf-8 (with a dash), and someplace as utf8 (without it). 注1 :请注意,它的位置标记为utf-8 (带破折号),某处为utf8 (没有它)。 It's important that you know when to use which, as they usually aren't interchangeable. 重要的是你知道何时使用哪种,因为它们通常是不可互换的。 For example, HTML and PHP wants utf-8 , but MySQL doesn't. 例如,HTML和PHP需要utf-8 ,但MySQL不需要。

Note 2 : In MySQL, "charset" and "collation" is not the same thing, see Difference between Encoding and collation? 注2 :在MySQL中,“charset”和“collat​​ion”不是一回事,请参阅编码和整理之间的区别? . Both should be set to utf-8 though; 两者都应该设置为utf-8; generally collation should be either utf8_general_ci or utf8_unicode_ci , see UTF-8: General? 通常整理应该是utf8_general_ciutf8_unicode_ci ,参见UTF-8:General? Bin? 滨? Unicode? Unicode的? .

Note 3 : If you're using emojis, MySQL needs to be specified with an utf8mb4 charset instead of the standard utf8 , both in the database and the connection. 注3 :如果您使用的是utf8mb4 ,则需要在数据库和连接中使用utf8mb4字符集而不是标准utf8指定MySQL。 HTML and PHP will just have UTF-8 . HTML和PHP只有UTF-8


Setting UTF-8 with mysql_ and PDO 使用mysql_和PDO设置UTF-8

  • PDO: This is done in the DSN of your object. PDO:这是在您的对象的DSN中完成的。 Note the charset attribute, 注意charset属性,

     $pdo = new PDO("mysql:host=localhost;dbname=database;charset=utf8", "user", "pass"); 
  • mysql_ : This is done very similar to mysqli_* , but it doesn't take the connection-object as the first argument. mysql_ :这与mysqli_*非常相似,但它不会将connection-object作为第一个参数。

     mysql_set_charset('utf8'); 

Solution: 解:

mysql_query("SET NAMES 'utf8';"); > $mysqli->set_charset('utf8'); > $mysqli->set_charset('utf8');

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

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