MySQL database garbled, how to deal with

Source: Internet
Author: User
MySQL database garbled
Store Chinese garbled, as if the character set is not uniform, how to set

$DB _name= "Students";

$table _name= "Classone";

$conn =mysql_connect (' localhost ', ' root ', ' abcdef ') or

Die ("The connection to the database server failed. ". Mysql_error ());

echo "Database server information:". Mysql_get_host_info ($conn). "
";

$mysql _command= "drop database if exists". $db _name;

$result =mysql_query ($mysql _command);

$mysql _command= "CREATE DATABASE". $db _name;

$result =mysql_query ($mysql _command) or

Die ("failed to establish database. ". Mysql_error ());

mysql_select_db ($db _name, $conn) or

Die ("Select Database failed. ". Mysql_error ());

echo "Database name: $db _name Data sheet name: $table _name
";

mysql_query ("SET NAMES ' GB2312 '");

$mysql _command= "CREATE TABLE". $table _name;

$mysql _command= $mysql _command. " (name varchar (), numb varchar (+), age int (6)) ";

$result =mysql_query ($mysql _command) or

Die ("Build data table: $table _name failed! ". Mysql_error ());

echo "Successfully built data sheet: $table _name
";

mysql_query ("SET NAMES ' GB2312 '");

$mysql _command= "INSERT into" $table _name. " (name,numb,age) VALUES (\ "";

$mysql _command= $mysql _command. " Liu Hua \ ", \" 200801\ ", 27)";

$result =mysql_query ($mysql _command) or

Die ("Data sheet: $table _name added record failed! ". Mysql_error ());

echo "Successfully added data sheet:". $table _name. The record. ";

?>


------Solution--------------------
Coding is commonly used in UTF8-GENERAL-CI
Not only the table to pass one, to unify the total of all around
One is: The database type, which includes, database, table, fields are unified three, you can check
The second is: the file encoding type, if you use DW or editplus can see the page encoding, different need to modify
The third is: when accessing the database settings are set NAMES UTF8;
Four is: the browser display mode, both
No matter with gbk,gb2312, this must be unified everywhere.
------Solution--------------------
OK, this is the question I met the day before yesterday, and my steps are this:
1. The code to be edited must be UTF-8 without BOM format
2. Set properties in the database, all fields are Utf-8
3. Add this code when you want to write into the database and read the database:
mysql_query ("Set character set ' Utf-8 '");
mysql_query ("Set names UTF8");
Doing a good job is to use the constructor to write this code.
------Solution--------------------
Coding is commonly used in UTF8-GENERAL-CI
Not only the table to pass one, to unify the total of all around
One is: The database type, which includes, database, table, fields are unified three, you can check
The second is: the file encoding type, if you use DW or editplus can see the page encoding, different need to modify
The third is: when accessing the database settings are set NAMES UTF8;
Four are: Browser display mode (HTML), both
Five is: Php file header Plus (Ajax calls sometimes need to convert functions)
PHP Code
Header (' content-type:application/x-www-form-urlencoded '); header (' Content-type:text/html;charset=utf-8 ');
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.