【问题标题】:PHP Cannot escape my string properlyPHP 无法正确转义我的字符串
【发布时间】:2009-12-10 19:46:48
【问题描述】:

我正在使用mysql_real_escape_string 来转义我的内容,但我在 SQL 插入查询中收到一个错误,因为单引号未转义。我该如何解决这个问题?

$content = mysql_real_escape_string("'content'",$conn);

我收到的错误信息是:

You have an error in your sql syntax near 'content

我的 SQL 查询最终成为以下内容:

$sql = "INSERT into `table` (`column`) VALUES ("'content'")

代替

$sql = "INSERT into `table` (`column`) VALUES ("\'content\'")

我还尝试使用单引号作为我的分隔符,但由于双引号没有被转义而失败。

【问题讨论】:

  • 您在 5 分钟前提出了这个问题。我们要求提供一些代码 sn-p。请。
  • 请完整引用您的错误信息。
  • 一个示例字符串真的很有帮助 - 你所描述的不应该是可能的。
  • @Anodyne :除非错误出现在 SQL 查询的另一部分;-) 就像插入的其余部分一样...这就是为什么人们一直要求 @Jonathan 提供更长的代码部分

标签: php mysql string escaping


【解决方案1】:

就像人们在你之前说的那样,你删除了,你需要给我们更多的信息,比如一个完整的例子,显示你查询构建的每个步骤;而且,您还应该自行使用 SQL 查询,以及您收到的错误消息...

不过,如果您允许我引用您之前的问题,您说您的 SQL 查询如下:

insert into `exp_weblog_data` (`entry_id`,`site_id`,`weblog_id`,`field_id_117`,`field_ft_117`,`field_id_27`,`field_ft_27`,`field_id_26`,`field_ft_26`,`field_id_28`,`field_ft_28`,`field_id_129`,`field_ft_129`,`field_id_33`,`field_ft_33`) 
values ("","1","112","Patch 1.10","none","","none","- Fixed a bug with certain Creative Lab DVD drives and copy protection.("Unable to connect to Battle.net").","none","","none","ftp://totukati.gamezone.com/lodpatch_110.exe","none","[16020] Diablo II: Lord of Destruction","none")

如果还是同一个查询,SQL中的字符串一定不能用双引号("),而是用单引号(')。

这意味着您的查询应该看起来更像这样:

insert into `exp_weblog_data` (`entry_id`,`site_id`,`weblog_id`,`field_id_117`,`field_ft_117`,`field_id_27`,`field_ft_27`,`field_id_26`,`field_ft_26`,`field_id_28`,`field_ft_28`,`field_id_129`,`field_ft_129`,`field_id_33`,`field_ft_33`) 
values ('','1','112','Patch 1.10','none','','none','- Fixed a bug with certain Creative Lab DVD drives and copy protection.("Unable to connect to Battle.net").','none','','none','ftp://totukati.gamezone.com/lodpatch_110.exe','none','[16020] Diablo II: Lord of Destruction','none')

【讨论】:

  • @Jonathan :(编辑您的其他问题)可能是一个解决方案;-)
  • @Pascal 现在我知道不要重新起草同样的问题 :)
猜你喜欢
  • 2023-03-26
  • 2014-04-27
  • 1970-01-01
  • 2014-08-02
  • 2011-01-05
  • 2019-07-28
  • 2017-01-31
  • 2011-08-23
  • 1970-01-01
相关资源
最近更新 更多