Home>Article>Backend Development> How to modify database encoding in php

How to modify database encoding in php

藏色散人
藏色散人 Original
2022-01-10 09:28:36 2521browse

php method to modify database encoding: 1. Create a user table in the mysql database; 2. Create a PHP file and connect to the mysql database; 3. Use the mysqli_query function to execute the "set names 'utf8'" sql statement , and set the encoding for connecting to the database to utf8.

How to modify database encoding in php

#The operating environment of this article: Windows 7 system, PHP version 7.1, Dell G3 computer.

How to modify the database encoding in php?

php mysql sets the encoding for connecting to the database

In the mysql database, create a user table and insert multiple pieces of data into the table for testing.

How to modify database encoding in php

Create a test.php file, and within the file, use the header() method to set the encoding format of the page to utf-8.

How to modify database encoding in php

In the test.php file, connect to the mysql database and use mysqli_select_db to select the database to operate.

How to modify database encoding in php

In the test.php file, use the mysqli_query function to execute the "set names 'utf8'" sql statement and set the encoding for connecting to the database to utf8 to avoid inserting Chinese into the database. Garbled characters.

How to modify database encoding in php

In the test.php file, use update to create a sql statement to change the username value of the record with id 1 to "test", and use the mysqli_query function Execute sql statement.

How to modify database encoding in php

Open the test.php file in the browser and view the results in the database.

How to modify database encoding in php

Recommended study: "PHP Video Tutorial"

The above is the detailed content of How to modify database 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