Home  >  Article  >  Backend Development  >  How to set mysql encoding in php?

How to set mysql encoding in php?

青灯夜游
青灯夜游Original
2020-11-06 17:34:102173browse

In PHP, you can use the mysql_query() function to set the mysql encoding, the syntax is "mysql_query('SET NAMES encoding method');"; the mysql_query() function needs to be placed after the mysql_connect() statement.

How to set mysql encoding in php?

In PHP, you can use the mysql_query() function to set the mysql encoding.

When PHP connects to the database, that is, after adding

//设置数据的字符集utf-8
mysql_query("set names 'utf8'");
mysql_query("set character_set_client=utf8");
mysql_query("set character_set_results=utf8");

after mysql_connect(), it is best to use utf-8 for the web page character set. UTF-8 is an international standard encoding and a trend.

If your web page encoding is gb2312, that is SET NAMES GB2312. However, the editor strongly recommends that web page encoding, MySQL data table character set, and PHPmyAdmin all use UTF-8.

Note:

The mysql_query("set names 'coding'"); encoding added before the PHP program that needs to perform database operations must be consistent with the PHP encoding. If PHP If the encoding is gb2312, then the mysql encoding is gb2312. If it is utf-8, then the mysql encoding is utf8, so that there will be no garbled characters when inserting or retrieving data.

mysqli_query()

In php, all sql statements are a string. This command string needs to be passed to mysqli_query() for execution.

mysqli_query(mysql, sql);

Explanation:

The mysql parameter is the connection object of mysql, the return value when the mysqli_connect() function is successfully executed.

The sql parameter is the sql command (string) to be executed

Returns true if successful, false if failed

Example:

1 (1).jpg

For more programming-related knowledge, please visit: Introduction to Programming! !

The above is the detailed content of How to set mysql encoding in php?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn