php connection database garbled code

王林
Release: 2023-02-24 22:04:02
Original
2463 people have browsed it

php connection database garbled code

To solve the problem of garbled characters in the PHP database, you can make the following settings:

1. Settings in the database:

(1 ) When creating a new database in MYSQL, the database selects UTF-8 encoding, the character set is set to utf-8_unicode_ci (Unicode (multi-language), case-insensitive), and the table arrangement in the library is set to utf-8_general_ci; in the table The collation of each field is set to utf-8_general_ci.

(2) When creating a database, specify the character type as uft8, such as:

create database db_name character set utf8;
Copy after login

or modify the created database to utf8 type:

alter database db_name character set utf8;
Copy after login

(3) When creating a table , the specified character type is utf8, such as:

CREATE TABLE tb_name(
 id int(10) NOT NULL auto_increment,
 username char(34) NOT NULL ,
 password int(56) NOT NULL,
 PRIMARY KEY (id)
 ) DEFAULT CHARSET=utf8;
Copy after login

or modify the created table to utf8 format:

alter table tb_name character set utf8;
Copy after login

or modify a field in the table to utf8 format

alter table tb_name modify type_name varchar(50) CHARACTER SET utf8;
Copy after login

2. PHP connection settings, add the statement in PHP’s MySQL connection function:

//注意此处为utf8,不要写成utf-8mysql_query("set names 'utf8'");
Copy after login

3. Page declaration encoding

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
Copy after login

4. Set the browser to utf-8 format.

5. Editor character encoding, the code document must be saved in utf-8 format.

6. If the inserted data is garbled, you can transcode the inserted data. First convert the string to utf-8 and then insert it into the database.

Recommended tutorial: PHP video tutorial

The above is the detailed content of php connection database garbled code. 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