我有一个表名作为测试。在测试表中,有列test1。 test1列的字符串值为“ abc&def”,我创建了一个字符串变量$ str =“ abc&def”。当我尝试执行类似查询时(从测试中选择*,其中test1如'%$ 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/

希望能帮助到你 :)

10-04 21:58
查看更多