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

藏色散人
Release: 2023-03-12 19:02:01
Original
1620 people have browsed it

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
Copy after login

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
Copy after login

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;
Copy after login

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");  //注意:这一句前不能向页面输出任何内容
Copy after login

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);
Copy after login
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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!