使用简单的功能:
mysqli_query($link, 'update table set field = \'variable text with \" slash\' ');
$res = mysqli_query($link, 'select field from table');
$res = mysqli_fetch_array($res);
打印结果时,我的字符串没有斜线,如:'变量文本带有“斜杠”。 当我在我的localhost mysql客户端(SequelPro)中预览表时,我发现也没有斜杠。 mysql在自动插入时删除此斜杠是正常的吗?这是一种防止这种情况的方法吗?我需要这个斜线。此外,我从db获取值后不能使用addslashes。 我的php服务器上禁用了魔术引号。
答案 0 :(得分:5)
您添加的斜杠用于转义php字符串中的引号。你应该添加3个斜杠\\\"
,这样前两个会产生一个反斜杠,第三个斜杠会引用它。
答案 1 :(得分:1)
我假设你想保留第二个反斜杠,因为第一个和第三个需要逃避单引号。要保持反斜杠,只需转义反斜杠:
mysqli_query($link, 'update table set field = \'variable text with \\" slash\' ');
您不需要转义双引号,因为此字符串由单引号括起来。
答案 2 :(得分:1)
你必须使用双sla \\
试试:
mysqli_query($link, "update table set field = 'variable text with \\" slash' ");
答案 3 :(得分:1)
这是因为PHP正在逃避"
。你也需要逃避反斜杠,如下所示:
mysqli_query($link, 'update table set field = \'variable text with \\\" slash\' ');
或者您可以使用addslashes()。
$query = addslashes('update table set field = \'variable text with \" slash\' ')
mysqli_query($link, $query);