Get the App
SLTechnology News&Howtos  ›  Development  › 

How to solve the problem that php mysql cannot query Chinese names

Shulou Source: shulou.com Published: 2022-06-03 10:40:08 10月01日 Update

This article introduces how to solve the problem that php mysql can not query Chinese names, the content is very detailed, interested friends can refer to, hope to be helpful to you.

Php mysql can not query the Chinese name of the solution: 1, to ensure that MySQL and PHP code consistent; 2, in the php page header added "header (" Content-type: text/html; charset=utf-8 ");".

This article operating environment: Windows7 system, PHP7.1, Dell G3 computer.

What if php mysql cannot query Chinese names?

1. Ensure that the codes of MySQL and PHP are the same.

MySQL

Modify the coding format of the entire database

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

Alter database test character set utf8

Modify the coding format of the whole table

Note: if the data is important, execute the following code to convert the entire table encoding format to utf8 (in this case, the data table is students)

Alter table students convert to character set utf8

Modify the encoding format of sname field in students table

Note: the first sanme is to change the encoding format for the sname field in the table.

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

Not null is modified sname cannot be empty. (you can choose not to add)

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

You can also use phpMyAdmin to enter the data table

Or access the database.

PHP

Add the following to the header of the php page

Header ("Content-type: text/html; charset=utf-8"); / / Note: you cannot output anything to the page before this sentence.

In addition, what is set in html is

two。 After getting the information passed by the search box, put in single quotation marks

Search box:

Enter a name:

Search

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

You can query Chinese character strings.

At least mine got it. ]

On how to solve the problem that php mysql can not query Chinese names is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Data coding format name query Chinese field database content page search question important consistent header datasheet more guarantee help good Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi MySQL Docker Shulou Information Apple