Home > Backend Development > PHP Tutorial > mysql - 下列PHP数据库insert语句中变量前后的点和双引号有什么作用?

mysql - 下列PHP数据库insert语句中变量前后的点和双引号有什么作用?

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 20:40:16
Original
1250 people have browsed it

<code> $query = "insert into books values
        ('".$isbn."', '".$author."', '".$title."', '".$price."')";
</code>
Copy after login
Copy after login

回复内容:

<code> $query = "insert into books values
        ('".$isbn."', '".$author."', '".$title."', '".$price."')";
</code>
Copy after login
Copy after login

这就要从双引号和单引号的作用讲起:
双引号里面的字段会经过编译器解释然后再当作HTML代码输出,但是单引号里面的不需要解释,直接输出。例如:
$abc='I love u';
echo $abc //结果是:I love u
echo '$abc' //结果是:$abc
echo "$abc" //结果是:I love u
所以在对数据库里面的SQL语句赋值的时候也要用在双引号里面SQL="select a,b,c from ..."
但是SQL语句中会有单引号把字段名引出来
例如:select * from table where user='abc';
这里的SQL语句可以直接写成SQL="select * from table where user='abc'"
但是如果象下面:
$user='abc';
SQL1="select * from table where user=' ".$user." ' ";对比一下
SQL2="select * from table where user=' abc ' "
我把单引号和双引号之间多加了点空格,希望你能看的清楚一点。
也就是把'abc' 替换为 '".$user."'都是在一个单引号里面的。只是把整个SQL字符串分割了。
SQL1可以分解为以下3个部分
1:"select * from table where user=' "
2:$user
3:" ' "
字符串之间用 . 来连接

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template