PHP operation MySQL Syntax format error

Source: Internet
Author: User
1、错误查询语句:$sql='select * from sh_admin where username=$username and password=$password limit 1';2、正确查询语句:$sql = "select * from sh_admin where username = '{$username}' and password = '{$password}' limit 1";

When PHP operation MySQL database Why must use ' {$username} ' way to query, PHP syntax double quotes can parse variables Ah, why do I use the first kind of error?

Reply content:

1、错误查询语句:$sql='select * from sh_admin where username=$username and password=$password limit 1';2、正确查询语句:$sql = "select * from sh_admin where username = '{$username}' and password = '{$password}' limit 1";

When PHP operation MySQL database Why must use ' {$username} ' way to query, PHP syntax double quotes can parse variables Ah, why do I use the first kind of error?

1, variables $username and $password in single quotes is not resolved
2. The values after the Character field username and password need to be quoted.

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

Your first SQL uses single quotes, so it's a mistake.
is to use

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

There's no problem.

Agree upstairs, the string is to be quoted.

Debugging will $sql output a bit, you can see at a glance!
The variable in the first single quotation mark is not parsed, and the string inside the SQL is 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.