How to specify the character set in PHP PDO

There are several ways to set the character encoding of the database. The recommended method is to append ; charset=charsetName to the dsn string. But earlier versions of PHP will ignore this option. Here, we will explore all available options to set the character encoding of the database:

 

One, specify the character set in the dsn string

To get the UTF-8 character set, you can specify it in the DSN :

$dsn = 'mysql:host=localhost; dbname=test; charset=UTF8';

$dbh = new PDO($dsn, $user, $password);

 

Two, by executing the specified character set  SET NAMES utf8

Before PHP 5.3.6 , the charset option was ignored. For older versions, please use the following method:

// For PHP versions lower than 5.3.6 )

$dbh = new PDO($dsn, $user, $password);

$dbh->exec('set names utf8');

If you also need to set the sorting rules:

$dbh->exec('set names utf8 COLLATE collateName');

// For utf8 , the default collation is utf8_general_ci

$dbh->exec('set names utf8 COLLATE utf8mb4_unicode_ci');

 

Three, specify the character set in the fourth parameter of the PDO constructor

You can also set the character encoding by creating an options-array for the fourth parameter of the PDO constructor :

// For PHP versions before 5.3.6 )

$options = [PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'];

$db = new PDO($dsn, $user, $password, $options);

Note: The above example only applies to MySQL database.

 

Fourth, output multi-byte (ie UTF-8 ) data

When dealing with multi-byte data (such as UTF-8 ), you must:

Use UTF-8 character encoding to store data.

Tell PHP to use UTF-8 character encoding. It 's easiest to do this in your php.ini file like this :

default_charset = "UTF-8";

 

Use UTF-8 character encoding to display data:

For PHP scripts and dynamically generated HTML files, send the content-type header: <?php

header('Content-Type: text/html; charset=utf-8');

 

Note: The header() function must be used before returning any output from PHP .

<!DOCTYPE html>

<html lang="en">

 <head>

  <meta charset="UTF-8">

  <meta name="viewport" content="width=device-width">

 

Configure the code editor or IDE to use UTF-8 character encoding.

 

Guess you like

Origin blog.csdn.net/allway2/article/details/109532315