In the process of developing a Web application, it is usually necessary to connect to a database to store, manage and retrieve data. While connecting to the database, we also need to ensure that the character set and encoding of the data meet our requirements to avoid problems such as garbled characters. In PHP development, we need to set the correct character set and encoding to ensure the correctness of database connection and data.
1. Setting the database character set
1.1 MySQL database character set setting
The character set of the MySQL database can be set in the following ways:
(1). Specify the character set when creating the database:
CREATE DATABASE mydb DEFAULT CHARACTER SET utf8;
In the above code, we use the "CREATE DATABASE" statement to create "mydb" database and use the "DEFAULT CHARACTER SET" option to specify the character set as UTF-8.
(2). Specify the character set when creating the data table:
CREATE TABLE mytable (
id INT, name VARCHAR(20), content TEXT
) CHARACTER SET utf8;
In the above code , we use the "CREATE TABLE" statement to create the "mytable" data table, and use the "CHARACTER SET" option to specify the character set as UTF-8.
(3). Specify the character set when modifying the data table:
ALTER TABLE mytable CONVERT TO CHARACTER SET utf8;
The above code will change the characters of the "mytable" data table Set modified to UTF-8.
(4). Specify the character set when connecting to the MySQL database:
$conn = mysqli_connect("localhost", "user", "password", "mydb");
mysqli_set_charset($conn, "utf8");
In the above code, we use the "mysqli_connect" function to connect to the database, and after the connection is successful, use the "mysqli_set_charset" function to specify the character set as UTF-8.
1.2 Character set setting of PostgreSQL database
The default character set of PostgreSQL database is "UTF-8", but it can also be modified in the following ways:
(1) . Specify the character set when creating the database:
CREATE DATABASE mydb ENCODING 'UTF8';
The above code will create a "mydb" database with a character set of UTF-8.
(2). Specify the character set when creating the data table:
CREATE TABLE mytable (
id INT, name VARCHAR(20), content TEXT
) WITH (OIDS=FALSE) TABLESPACE pg_default ENCODING 'UTF8';
In the above code, we use the "CREATE TABLE" statement to create the "mytable" data table, and use the "ENCODING" option to specify the character set as UTF-8.
(3). Specify the character set when connecting to the PostgreSQL database:
$conn = pg_connect("host=localhost dbname=mydb user=user password=password");
pg_set_client_encoding ($conn, "UTF8");
In the above code, we use the "pg_connect" function to connect to the database, and after the connection is successful, use the "pg_set_client_encoding" function to specify the character set as UTF-8.
2. Set the PHP script character set
In PHP, we also need to set the script character set to ensure that it is consistent with the character set of the database. In PHP, you can set the script character set in the following way:
header("Content-Type: text/html; charset=utf-8");
The above code uses "header" Function to set the character set of script output to UTF-8.
3. Set the character set of the HTML page
Finally, the character set also needs to be set in the HTML page to ensure that it is consistent with the character set of the PHP script and database. In an HTML page, the character set can be set in the following way:
The above code uses the "meta" tag to set the character set of the HTML page to UTF-8.
Summary
In the process of connecting to the database, you need to ensure that the character sets of the database, PHP script and HTML page are consistent to avoid problems such as garbled characters. The above describes how to set character sets in MySQL and PostgreSQL databases, PHP scripts and HTML pages, and provides corresponding sample codes for readers' reference.
The above is the detailed content of How to connect to the database in php and set the character set encoding. For more information, please follow other related articles on the PHP Chinese website!