개발! 딱 깔끔하고 센스있게!

MySQL 및 PHP : 키릴 문자가있는 UTF-8 본문

개발 스크랩 메모/PHP

MySQL 및 PHP : 키릴 문자가있는 UTF-8

렉사이 2020. 12. 12. 02:36

나는 mysql 시계에 시릴치를 삽입하려고 했지만 인코딩이 문제가 있다.

필리핀 피소:

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 ав | 

주: php 파일은 utf-8, 데이터베이스 정렬은 utf8u generalu ci

대답

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.

너의 실제 문제는 어느 곳의 문자 집합 문제이다.

여기에 몇몇 지침들이 프로그램을 위해 정확한 문자 집합을 얻을 수 있도록 도와줄 수 있습니다.

php/mysql 응용 프로그램을 개발할 수 있는 대부분의 일반 문제가 포함되어 있습니다.

  • 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 tags):

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

전체 코드 파이프의 모든 내용은 uft-8 설정이 필요합니다.

그렇지 않으면 프로그램에서 끊기는 문자가 생길 수 있습니다.

이 목록 외에도 일부 함수는 지정 문자 집합에 사용할 특정 인자를 가지고 있을 수 있습니다.

설명서는 이 점을 알려드릴 것입니다.

여러 바이트 문자를 겨냥한 특수함수는 예를 들어: strToLower () 가 여러 바이트 문자의 값을 낮추지 않으므로 mb strToower () 를 사용해야 합니다.

이 실시간 시사회를 참고하십시오.

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.


mysql 및 pdo 설정 utf-8

  • 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'); 
Comments