Home  >  Article  >  Backend Development  >  The field value written by PHP is garbled

The field value written by PHP is garbled

王林
王林Original
2019-09-28 17:59:251970browse

The field value written by PHP is garbled

1. Use phpmyadmin to create the database and data table

When creating the database, please set "Organization" to: " utf8_general_ci" or execute the statement:

CREATE DATABASE `dbname` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

When creating a data table: If the field stores Chinese, you need to set "Organization" to: "utf8_general_ci", if the field stores English or numbers. , the default is fine.

Corresponding SQL statement, for example:

CREATE TABLE `test` (

`id` INT NOT NULL ,

`name` VARCHAR( 10 ) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ,

PRIMARY KEY ( `id` )

) ENGINE = MYISAM ;

2. Use PHP to read and write the database

After connecting to the database

$connection = mysqli_connect($host_name, $host_user, $host_pass);

Join:

mysqli_query("set character set 'utf8'");//读库

mysqli_query("set names 'utf8'");//写库

You can read and write the MYSQL database normally.

The environment used is appserv-win32-2.5.10. When installing this package, the default utf8 encoding is used.

When writing the database connection file, write:

$conn = mysqli_connect("$host","$user","$password");

mysqli_query("SET NAMES 'UTF8'");

mysqli_select_db("$database",$conn);

Then when making the page, pay attention to this sentence:

In this way, regardless of the Chinese input into the database or the page display, everything will be normal.

In the DW CS4 version, the utf8 page is generated by default. Similarly, if you first write the database connection file, you write:

mysqli_query("SET NAMES 'GBK'");

The page should also change accordingly:

Recommended tutorial: PHP video tutorial

The above is the detailed content of The field value written by PHP is garbled. 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