Related issues with PHP connection database

Source: Internet
Author: User
Tags database issues
PHP Connection to database issues
$link =mysql_connect ("localhost", "root", "906200");
mysql_select_db ("Manage");
$sql = "SELECT * from commodity where commodity_name=". $name;/////////Error said this sentence is not right!!!???
$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 previous page has been verified to pass the value successfully to $name, error saying that you had an error in your SQL syntax; Check the manual that corresponds to your MySQL server version for the right syntax to use near "select * from commodity Where commodity_name= ' cookies ' at line 1

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




------Solution--------------------
$sql = "SELECT * from commodity where commodity_name= ' $name '";
------Solution--------------------
Find/Update/INSERT fields if a type such as varchar,char,text needs to enclose the content in quotation marks.
------Solution--------------------
$sql when the error is posted

Big5_chinese_ci???
What code is your $name?
------Solution--------------------
= Inconsistent coding on both sides one is latin1_swedish_ci one is big5_chinese_ci
Note Unified encoding
------Solution--------------------
Add: mysql_query ("Set names GBK") when you connect to the database;
------Solution--------------------
Utf8_general_ci
Then your php,html files are saved in the UTF-8 format. There will be no more garbled problems.
------Solution--------------------
Say is, each one changed to Utf8_general_ci, this is the most common
Note: For int type variables, do not change
The main change is three points,
One is the database type has a place
The second is that the table attribute has a place
Third, there is a place for each field non-int
After connecting to the database
mysql_query ("Set NAMES utf-8");
In the future all the Chinese operation will not need you to worry about the coding problem appears
(Pay more attention to chatset=utf-8; or PHP page attribute dw to Utf-8)
------Solution--------------------
Windows environment is full use of the GBK,LINUX environment is full use of UTF8, including HTML http-equiv, database encoding, PHP file encoding (same as the system locale).
------Solution--------------------
Unified encoding

First, determine the character encoding of your file, second, determine the input and output encoding of your database connection, and third, determine the character encoding of your database, table, field. These pills are all consistent.

It's best 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.