Home >Backend Development >PHP Problem >What should I do if php mysql cannot query Chinese names?

What should I do if php mysql cannot query Chinese names?

藏色散人
藏色散人Original
2021-09-20 09:21:011681browse

php MySQL cannot query the Chinese name solution: 1. Ensure that the encoding of MySQL and PHP is consistent; 2. Add "header("Content-type: text/html; charset=utf to the header of the PHP page -8");".

What should I do if php mysql cannot query Chinese names?

The operating environment of this article: Windows7 system, PHP7.1, Dell G3 computer.

#php What should I do if mysql cannot query Chinese names?

1. Ensure that the encoding of MySQL and PHP is consistent

MySQL

Modify the encoding format of the entire database

Note : If the data is not important, you can directly change the encoding format of the database and then rebuild the table. (The database in this example is test)

alter database test character set utf8

Modify the encoding format of the entire table

Note: If the data is important, execute the following code to convert the encoding format of the entire table to utf8 That’s it (the data table in this example is students)

alter table students convert to character set utf8

Modify the encoding format of the sname field in the students table

Note: The first sanme is to modify the encoding of the sname field in the table Format.

The second sname varchar(64) is the field name and field type to be modified (generally the same as in the original table, no modification will be made)

not null is to modify the sname and cannot be empty. . (You can choose not to add it)

alter table students change sname sname varchar(64) character set utf8 not null;

You can also use phpMyAdmin to enter the data table

What should I do if php mysql cannot query Chinese names?

## or enter the database

What should I do if php mysql cannot query Chinese names?

PHP

Add in the header of the php page:

header("Content-type: text/html; charset=utf-8");  //注意:这一句前不能向页面输出任何内容

In addition, the setting in the html is

2. After obtaining the information passed by the search box, Add single quotes

to the search box:

Enter the name:

Search

xxx.php:
$tofind = $_POST["userName"];
$sql = "SELECT * FROM user WHERE  name='$tofind';";
$result = $conn->query($sql);

to query the Chinese character string

【 At least mine is done. . 】

Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of What should I do if php mysql cannot query Chinese names?. 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