An error occurred while operating mysql syntax in php.

Source: Internet
Author: User
{Code ...} why can't I use the & #039; {$ username} & #039; Method to query mysql Databases in PHP? variables can be parsed using double quotation marks in PHP syntax, why do I encounter errors when I use the first one?
1. Error query statement: $ SQL = 'select * from sh_admin where username = $ username and password = $ password limit 1'; 2. correct query statement: $ SQL = "select * from sh_admin where username = '{$ username}' and password = '{$ password}' limit 1 ";

Why can't I use the '{$ username}' method to query mysql Databases in PHP? The variable can be parsed using double quotation marks in PHP syntax. Why can't I use the first one?

Reply content:
1. Error query statement: $ SQL = 'select * from sh_admin where username = $ username and password = $ password limit 1'; 2. correct query statement: $ SQL = "select * from sh_admin where username = '{$ username}' and password = '{$ password}' limit 1 ";

Why can't I use the '{$ username}' method to query mysql Databases in PHP? The variable can be parsed using double quotation marks in PHP syntax. Why can't I use the first one?

1. Variables $ username and $ password are not parsed in single quotes.
2. The values following the username and password fields must be enclosed in quotation marks.

$sql='select * from sh_admin where username="'.$username.'" and password="'.$password.'" limit 1';

Your first SQL statement uses single quotes, so an error occurs.
Yes

$sql="select * from sh_admin where username=$username and password=$password limit 1";

No problem.

Agree that the string is enclosed in quotation marks.

After debugging$sqlOutput it at a glance!
The variables in the first single quotes are not parsed, And the strings in the SQL statement are to be caused.

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.