我正在尝试在MySQL数据库中插入一个字符串-它包含包含引号、星号、反斜杠等的正则表达式-以下不起作用-有什么想法如何更正它?我也尝试过基于其他答案的毫无乐趣的htmlentities。

qa_db_query_sub('INSERT INTO `^islamiqa_topics` (title, description, regexp) VALUES ($, $, $)', $title, $description, mysql_real_escape_string($regexp));

这是我得到的错误:
Database query error 1064
You have an error in your SQL syntax;
check the manual that corresponds to your
MySQL server version for the right syntax to use near 'regexp)
VALUES ('7/7', '3rd of March 1924 was the day the Ottoman' at line 1

INSERT INTO `qa_islamiqa_topics` (title, description, regexp) VALUES ('7/7', '3rd of March 1924 was the day the Ottoman Caliphate was formally abolished in Turkey by Mustafa Kemal Atataurk.', '7/7(/2005)?|7(th)? july 2005|july 7.* 2005')

最佳答案

在将字符串添加到数据库之前,可以对其进行base64_encode()编码。

qa_db_query_sub('INSERT INTO `islamiqa_topics` (`title`, `description`, `regexp`) VALUES ($, $, $)', $title, $description, base64_encode($regexp));

另外,@Del发现regexp是一个保留字,因此不能将它用作表列名。您也可以尝试使用regexp(添加引号)

09-10 08:06
查看更多