我有一些自动化的工作流程,其中包括通过SQL更新带有HTML标记的列。
基本SQL语句如下所示:
UPDATE content SET bodytext = '<div class="one two three">Here comes a whole lot of HTML with all special chars and double quotes " and single quotes ' and empty lines and all possible kind of stuff...</div>' WHERE pid = 10;有没有办法让MariaDB或MySQL在SQL中自动转义(不用PHP)?
发布于 2020-04-28 00:32:08
我建议使用准备好的语句。通过这种方式,您可以将语句与其参数分离,并且不需要关心在普通SQL中所需的额外转义。
使用PHP的MySQLi驱动程序中提供的功能将简化该过程:
https://www.w3schools.com/php/php_mysql_prepared_statements.asp
在普通SQL中也可以使用预准备语句,但我不确定是否值得手动执行这些语句
https://dev.mysql.com/doc/refman/8.0/en/sql-prepared-statements.html
发布于 2020-04-28 03:07:52
谢谢你的意见,但我想,我找到了一个适合我的解决方案。看起来您实际上可以通过这种语法告诉SQL服务器接受一个原始字符串:
SELECT q'[The 'end' of the day]'
(来源:https://www.databasestar.com/sql-escape-single-quote/)
因此,我做了以下工作:
SELECT @html := '[<div class="one two three">Here comes a whole lot of HTML with all special chars and double quotes " and single quotes '' and empty lines and all possible kind of stuff...</div>]';
UPDATE content SET bodytext = @html WHERE pid = 10;它以这种方式工作,没有任何逃脱的问题。
https://stackoverflow.com/questions/61462173
复制相似问题