在字符串中包含单引号 [英] Include single quote to string
问题描述
我想从MySQL数据中下拉一个我要<option value ='and acode ='Aircraft1''>
的地方,包括将其传递给查询的两个单引号.我已经加入了反斜杠,但是它不起作用.我的代码有什么问题?
I want to have a dropdown from MySQL data where I want <option value ='and acode ='Aircraft1''>
, including the two single quotes to pass it into a query. I'm including backslash already but it's not working. What's wrong with my code?
<?php
$asql = "SELECT * FROM aircraft";
$aresult = mysql_query($asql);
?>
Aircraft:
<select name="aircraft">
<option value="and (acode = 'RP-C1086' or acode='RP-C1728')" checked/>All</option>
<?php while($arow=mysql_fetch_array($aresult))
{
echo "<option value='and acode=\'".$arow['reg']."\''>".$arow['reg']."</option>";
} ?>
</select>
推荐答案
来自 MySQL手册:
有几种方法可以在字符串中包含引号字符:
There are several ways to include quote characters within a string:
-
用
'
引用的字符串中的'
可能写为''
.
A
"'"
inside a string quoted with"'"
may be written as"''"
.
用"
引用的字符串中的"
可能写为""
.
A """
inside a string quoted with """
may be written as """"
.
在引号字符前加上转义字符(\
).
Precede the quote character by an escape character ("\"
).
用"
引用的字符串中的'
不需要特殊处理,不需要加倍或转义.以同样的方式,用'
引用的字符串内的"
不需要特殊处理.
A "'"
inside a string quoted with """
needs no special treatment and need not be doubled or escaped. In the same way, """
inside a string quoted with "'"
needs no special treatment.
使用php进行编码时,请记住在字符串文字中使用\\
以在字符串值中获得单个\
.
When coding in php, remember to use \\
in a string literal to get a single \
in the string value.
如果要转义HTML属性中的引号,请将'
替换为"
.或者,用"
引用属性值.这些是无论字符串值来自何处都可以使用的技术.参见此线程.
If you want to escape the quote in an HTML attribute, replace the '
with "
. Alternatively, quote the attribute value with "
. These are the techniques to use regardless of where the string value came from. See this thread.
这篇关于在字符串中包含单引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!