Home>Article>Backend Development> How to solve the problem of Chinese garbled characters in PHP reading database

How to solve the problem of Chinese garbled characters in PHP reading database

藏色散人
藏色散人 Original
2021-09-15 09:54:41 3884browse

Solution to php reading garbled Chinese characters from the database: 1. Open the corresponding code file for PHP to read the database; 2. Add the "mysql_query("set names 'utf8'");" code.

How to solve the problem of Chinese garbled characters in PHP reading database

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

How to solve the Chinese garbled code in php reading database question?

The problem of Chinese garbled characters when reading and writing the database in PHP

The solution to the problem of Chinese garbled characters when reading and writing the database in PHP:

Just add a sentence and it will be fine :

    mysql_query("set character set 'utf8'");//读库       mysql_query("set names 'utf8'");//写库       //其实读写都可以只加入       mysql_query("set names 'utf8'");

If you use mysqli, it is actually the same statement, but the main thing to note is that you must not write the character set as UTF-8, which is not available in PHP syntax.

mysql_query()Function executes a MySQL query.

Syntax

mysql_query(query,connection)

Parameters

query required. Specifies the SQL query to be sent. Note: The query string should not end with a semicolon.

connection Optional. Specifies the SQL connection identifier. If not specified, the last opened connection is used.

Description

If there is no open connection, this function will try to call the mysql_connect() function without parameters to establish a connection and use it.

Return value

mysql_query() returns a resource identifier only for SELECT, SHOW, EXPLAIN or DESCRIBE statements, or FALSE if the query is executed incorrectly.

For other types of SQL statements, mysql_query() returns TRUE when executed successfully and FALSE when an error occurs.

A non-FALSE return value means the query is valid and can be executed by the server. This does not say anything about the number of rows affected or returned. It's possible that a query executed successfully but did not affect or return any rows.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to solve the problem of Chinese garbled characters in PHP reading database. 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