MYSQL statement doubt

Source: Internet
Author: User
Tags parse error
MYSQL statement doubt $ SQL = "select * from news where id = '". $ _ GET ['id']. "'";
$ Query = mysql_query ($ SQL );
$ Rs = mysql_fetch_array ($ query );


Why is an error occurred when writing the preceding query statement? $ SQL = "select * from news where id = $ _ GET ['id']";
Thank you for your understanding of the difference between single quotes and double quotes!


Reply to discussion (solution)

You can add {}
$ SQL = "select * from news where id = {$ _ GET ['id']}";

$ SQL = "select * from news where id = $ _ GET ['id']";

This should be correct. what error is prompted ??

You can add {}
$ SQL = "select * from news where id = {$ _ GET ['id']}";

Can you explain it? Why not?

$ SQL = "select * from news where id = $ _ GET ['id']";

This should be correct. what error is prompted ??

Error: Parse error: syntax error, unexpected ''(T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C: \ Program Files \ Apache Software Foundation \ Apache2.2 \ htdocs \ just \ edit. php on line 5

This is a php convention. why?
When a member of an associated array appears in double quotes, you must write
"$ _ GET [id]"
Or
"{$ _ GET ['id']}"

This is a php convention. why?
When a member of an associated array appears in double quotes, you must write
"$ _ GET [id]"
Or
"{$ _ GET ['id']}"

Well, no error will be reported for either of your two formats.
$ Nc =$ _ POST [nc];
$ SQL = mysql_query ("select * from tb_user where name = '". $ nc. "'", $ conn );

$ Nc is a common variable that accepts the value assigned by $ _ POST [nc]. why do you want to write it like this? "select * from tb_user where name = '". $ nc. "'"

Echo $ SQL; everything is clear

Why do you want to write this statement like this? "select * from tb_user where name = '". $ nc ."'"
That's because he doesn't know or is used to his original language.
Of course, it may be because you have listened to others' misleading ideas.
"Select * from tb_user where name = '$ nc '"
Low efficiency due to addressing
As everyone knows, "select * from tb_user where name = '". $ nc ."'"
Is there an addressing process?

Oh, I thought it was fixed like that. This PHP single double quotation mark is mixed with C and C ++. String and a single character. Thank you!

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.