mysql - 如果字符串包含符号 '&',则类似查询不起作用

标签 mysql sql-like ampersand

我有一个表名作为测试。在测试表中有列 test1。 test1 列具有字符串值“abc & def”,我创建了一个字符串变量 $str =“abc & def”。当我尝试执行类似查询时(select * from test where test1 like '%$str%')。 这不会产生任何结果。有人可以帮忙吗?

最佳答案

假设您正在使用核心(普通)PHP:

如下所示:

$esc_str = $db->quote($str);
// this is for PDO; for MySQLi use $db->escape_string($str) instead
$qry = "select * from test where test1 like '%$esc_str%'"

使用以下内容:

$esc_str = $db->quote($str);
$qry = "select * from test where test1 like '%".$esc_str."%'"

$esc_str = $db->quote($str);
`select * from test where test1 like "%$esc_str%"`

此示例将 php 变量转换为 https://www.virendrachandak.com/techtalk/php-double-quotes-vs-single-quotes/

希望有帮助:)

关于mysql - 如果字符串包含符号 '&',则类似查询不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52314620/

相关文章:

MySQLi : SELECT priority using OR

mysql - 如何在mysql中选择一个时间段?

php - 从html到MYSQL的几个名字

PHP 错误无法从 Android 应用程序中检索 LIKE 语句

带有意外 "amp"和 ";"的 python url

php - 检查字段是否已填写,否则在 php 中显示错误

SQL `LIKE` 复杂度

php - 简单的PHP查询题: LIKE

html - 电子邮件地址中的符号 (href)

r - 在 R 中重载 & 运算符