Home>Article>Backend Development> How to modify database encoding in php
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.
#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.
Create a test.php file, and within the file, use the header() method to set the encoding format of the page to utf-8.
In the test.php file, connect to the mysql database and use mysqli_select_db to select the database to operate.
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.
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.
Open the test.php file in the browser and view the results in the database.
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!