Transmitting Non-ASCII Characters with Database

This section describes some basic settings and rules on how to transmit non-ASCII characters from PHP scripts to MySQL database server, and from MySQL database server back to PHP scripts.

Handling non ASCII characters with MySQL not only requires us setting up the table columns with the correct encoding (character set), but also requires us setting up the correct encoding for transmitting characters to and from the database.

MySQL offers the following global variables to control the encodings used to transmit characters between the client (PHP script) and the server (MySQL database):

In most cases, you should make the client (PHP script) to use the same encoding (character set) as the server (MySQL server), so that there is no need to convert when transmitting characters between them.

Some times, you may need to use different encodings. For example, you want to use UTF-8 for your PHP scripts, while using GB2312 for your MySQL tables.

You can use two special SET commands to change those variables:

1. "SET NAMES 'x'" is equivalent to these three statements:

   SET character_set_client = x;
   SET character_set_results = x;
   SET character_set_connection = x;

2. "SET CHARACTER SET x" is equivalent to these three statements:

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

You can always use the SHOW VARIABLES LIKE 'variable_name' to view the current value of the given variable.

Last update: 2015.

Table of Contents

 About This Book

 Introduction and Installation of PHP 7.0

 PHP Script File Syntax

 PHP Data Types and Data Literals

 Variables, References, and Constants

 Expressions, Operations and Type Conversions

 Conditional Statements - "if" and "switch"

 Loop Statements - "while", "for", and "do ... while"

 Function Declaration, Arguments, and Return Values

 Arrays - Ordered Maps

 Configuring and Sending out Emails

 Retrieving Information from HTTP Requests

 Creating and Managing Sessions in PHP Scripts

 Sending and Receiving Cookies in PHP Scripts

 Controlling HTTP Response Header Lines in PHP Scripts

 MySQL Server Connection and Access Functions

 Functions to Manage Directories, Files and Images

 SOAP Extension Function and Calling Web Services

 SOAP Server Functions and Examples

 Localization Overview of Web Applications

 Using Non-ASCII Characters in HTML Documents

 Using Non-ASCII Characters as PHP Script String Literals

 Receiving Non-ASCII Characters from Input Forms

 "mbstring" Extension and Non-ASCII Encoding Management

Managing Non-ASCII Character Strings with MySQL Servers

 Storing Non-ASCII Characters in Database

Transmitting Non-ASCII Characters with Database

 MySqlUnicode.php - MySQL Unicode UTF-8 Encoding Example

 Introduction of Class and Object

 Integrating PHP with Apache Web Server

 Outdated Tutorials

 References

 PDF Printing Version