php - 关于查找文本的 mysQL 查询帮助

标签 php sql mysql escaping

我需要获取 eventname 字段包含此文本(包括单引号)的所有行

's Birthday

我试过了,但它给出了错误:

select * from fab_scheduler where eventname like '%\'s Birthday%'

我如何构建这样的查询?

谢谢

最佳答案

如果您打算在 SQL 中包含 's,则需要 escape它:

There are several ways to include quote characters within a string:

  • A “'” inside a string quoted with “'” may be written as “''”.
  • A “"” inside a string quoted with “"” may be written as “""”
  • Precede the quote character by an escape character (“\”)
  • A “'” inside a string quoted with “"” needs no special treatment and need not be doubled or escaped. In the same way, “"” inside a string quoted with “'” needs no special treatment.

此外,如果您在程序中构建 SQL,您应该强烈考虑使用 mysql_real_escape_string 转义进入 SQL 查询的变量。或其在您的编程语言中的等价物。如果不这样做,您的应用程序将容易受到控制所述变量数据源的任何人(可能是您的用户;请记住,“您的用户”可能意味着“整个互联网”,具体取决于具体情况)的 SQL 注入(inject)攻击。

编辑:引用上面的dev.mysql.com说明。

关于php - 关于查找文本的 mysQL 查询帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2811593/

相关文章:

mysql - 外键问题

mySQL:使用 IN 语句时可以依赖 mySQL 完成的隐式 ORDER BY 吗?

php - Flash 被 ISP 阻止

mysql - 请帮我优化这个 MySQL SELECT 语句

SQL Server : check if variable is Empty or NULL for WHERE clause

mysql - 如何使用单个查询更新两行的交换值

mysql - MySQL中按多个时间戳排序

php - 使用php从android访问数据库获取 "<HTML>"而不是查询结果

php - 在 PHP 中不使用 SMTP 类发送附件邮件的最佳方法

javascript - 自定义选项更新 Magento 产品图像