An error occurred while executing the table creation statement containing Chinese characters in php.

Source: Internet
Author: User
Failed to execute the table creation statement containing Chinese characters in php $ conn = mysql_connect ("localhost", "root ","");
If ($ conn ){
Mysql_select_db ("test", $ conn );
$ SQL = "create table user
(
Id int (5) not null auto_increment primary key,
Name char (10) not null default '',
Password char (12) not null default '',
Age int (3) not null default 0,
Sex char (10) not null default 'male ',
Mail char (50) not null default '',
Degree char (10) not null default''
);";
$ Do = mysql_query ($ SQL, $ conn );
If ($ do ){
Echo "operation successful ";
}
Else {
Echo "operation failed ";
}
} Else {
Echo "an error occurred while connecting to the server ";
}
Run the above code and return "operation failed ". I tested it myself and found it was a problem with Chinese characters, that is, the default sex field is 'male. However, if you run the statements in $ SQL separately in cmd, you can successfully create a table.
I don't know what is going on. I hope you can answer it.


Reply to discussion (solution)

Character set problems
After connecting to the database, run
Mysql_query ('set names ??? ');//??? Encode your program File (utf8 should be written for UTF-8)

Add default charset = gbk to the table creation statement

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.