如何在MySQL数据库中存储包含引号的字符串?

问题描述:

我有下表:

$sql = "CREATE TABLE received_queries
    (
     sender_screen_name varchar(50),
     text varchar(150)
    )";

我使用以下SQL语句将值存储在表中

I use the following SQL statement to store values in the table

$sql = "INSERT INTO received_queries VALUES ('$sender_screen_name', '$text')";

现在,我正在尝试将以下字符串存储为文本".

Now I am trying to store the following string as 'text'.

又一个#haiku:心目中的棉线;睡不着觉的感觉; 我需要咖啡.

One more #haiku: Cotton wool in mind; feeling like a sleep won't cure; I need some coffee.

,我收到以下错误消息:

and I get the following error message:

错误:您的SQL语法有错误;检查手册 对应于您的MySQL服务器版本以使用正确的语法 几乎无法治愈;我需要一些咖啡.')'在第1行

Error: 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 't cure; I need some coffee.')' at line 1

我认为这一定是一个非常普遍的问题.我该怎么解决?

I think this must be a pretty common problem. How do I solve it?

我建议使用准备好的语句(使用mysqli或pdo),而不要转义.

I would suggest using prepared statements (with mysqli or pdo) rather than escaping.