如何在MySQL数据库中存储包含引号的字符串? [英] How do I store a string containing quotes in a MySQL database?

查看:846
本文介绍了如何在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.

这篇关于如何在MySQL数据库中存储包含引号的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆