dongpa9277 2015-08-08 18:53
浏览 22
已采纳

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

I'm trying to insert a Cyrillic value in the MySQL table, but there is a problem with encoding.

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:

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

Note: PHP file is utf-8, database collation utf8_general_ci

  • 写回答

2条回答 默认 最新

  • dongtangze6393 2015-08-09 00:19
    关注

    You are mixing APIs here, mysql_* and mysqli_* doesn't mix. You should stick with mysqli_ (as it seems you are anyway), as mysql_* functions are deprecated, and removed entirely in 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.

    • ALL attributes throughout your application must be set to 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)
    • The header in both PHP and HTML should be set to UTF-8

      • HTML (inside <head></head> tags):

        <meta charset="UTF-8">
        
      • PHP (at the top of your file, before any output):

        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)

      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).

    • Also make sure your database and tables are set to UTF-8, you can do that like this:

      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.

    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.

    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()).

    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.

    Note 1: Notice that its someplace noted as utf-8 (with a dash), and someplace as utf8 (without it). 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.

    Note 2: In MySQL, "charset" and "collation" is not the same thing, see Difference between Encoding and collation?. Both should be set to utf-8 though; generally collation should be either utf8_general_ci or utf8_unicode_ci, see UTF-8: General? Bin? 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. HTML and PHP will just have UTF-8.


    Setting UTF-8 with mysql_ and PDO

    • PDO: This is done in the DSN of your object. Note the charset attribute,

      $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_set_charset('utf8');
      
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)