我有一个表名作为测试。在测试表中有列 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/