简体   繁体   中英

php - the single quotes mixture with the double quotes use

     <?php
       $query = "SELECT * ";
       $query .= "FROM subjects ";
       $query .= "WHERE id='" . $subject_id ."' ";;
       $query .= "LIMIT 1";
     ?>

I have a problem with this line :

     <?php 
       $query .= "WHERE id='" . $subject_id ."' "; 
     ?>

When i used the double quotes only it gave me this error : "Database query failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LIMIT 1' at line 1"

So what's is the use of using the single quotes inside the double quotes ?

The use of single quotes within in double quotes is that you also need to pass a string to MySQL. PHP and MySQL both work with single and double quotes as string delimiters.

Compare the two lines:

 $query .= "WHERE id='" . $subject_id ."' ";

This is MySQL string (delimiters are single quotes) in a PHP String (delimiters are double quotes).

 $query .= "WHERE id=" . $subject_id ." ";

This time there is NO MySQL string, only a PHP String.

Assuming column id is a numeric field, you would use the latter variant.

Possibly just an error in the question, but this line has 2 semicolons:

$query .= "WHERE id='" . $subject_id ."' ";;

And if you skip the concatenation, this should work

$query .= "WHERE id='$subject_id' ";

In PHP, inside double quotes, single quotes are literal and variables are expanded.

The other way you wrote it should work too, but it's more code than needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM