Php database connection problems

Source: Internet
Author: User
Php connection to the database $ link = mysql_connect (& quot; localhost & quot;, & quot; root & quot;, & quot; 906200 & quot;); & nbsp; & nbsp; mysql_select_db (& quot; manage & quot;); $ SQL = & quot; s php connection to the database
$ Link = mysql_connect ("localhost", "root", "906200 ");
Mysql_select_db ("manage ");
$ SQL = "select * from commodity where commodity_name =". $ name; // The error message !!!????
$ Result = mysql_query ($ SQL) or die (mysql_error ());
$ Num = mysql_num_rows ($ result );
Echo $ num;
If ($ num> 0 ){
While ($ rs = mysql_fetch_object ($ result ))
{
$ C_id = $ rs-> commodity_id;
$ C_name = $ rs-> commodity_name;
$ C_kind = $ rs-> commodity_kind;
$ C_price = $ rs-> commodity_buyprice;
Echo"". $ C_id ."". $ C_name ."". $ C_kind ."". $ C_price ."";
}
Mysql_close ();
}



The value of $ name has been verified on the previous page. the error "You have an error in your SQL syntax" is returned; check the manual that corresponds to your MySQL server version for the right syntax to use near ''select * from commodity where commodity_name = 'cooker' at line 1

This is why logging ...... (The table name is commodity)




------ Solution --------------------
$ SQL = "select * from commodity where commodity_name = '$ name '";
------ Solution --------------------
For varchar, char, and text fields, enclose the content in quotation marks.
------ Solution --------------------
Paste $ SQL when an error occurs

Big5_chinese_ci ???
What is your $ name encoded?
------ Solution --------------------
= The encoding on both sides is inconsistent. one is latin1_swedish_ci and the other is big5_chinese_ci.
Pay attention to unified encoding
------ Solution --------------------
When you connect to the database, add: mysql_query ("set names gbk ");
------ Solution --------------------
Utf8_general_ci
Then your php and html files are saved in UTF-8 format. There will be no garbled characters.
------ Solution --------------------
It is said that it is most common to change one by one to utf8_general_ci.
Note: You do not need to change the int type variable.
Three major changes,
First, there is a place for the database type
Second, there is a place for table attributes.
Third, each field is not of the int type.
After connecting to the database
Mysql_query ("set NAMES UTF-8 ");
In the future, you will not need to worry about encoding problems for all Chinese operations.
(Pay more attention to chatset = UTF-8; or change the php page attribute dw to UTF-8)
------ Solution --------------------
In windows, gbk is used, and utf8 is used in linux, including http-equiv in html, database encoding, and PHP file encoding (same as locale in the system ).
------ Solution --------------------
Unified encoding

First, determine the character encoding of your file. second, determine the encoding of input and output when your database is connected. third, determine the character encoding of your database, table, and field. All these medicines are consistent.

It is better to use UTF-8. I usually use utf8_general_ci

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.