我不能将SQL LIKE
与变量
$sql = "SELECT * FROM chat WHERE name LIKE 'Motherboard' "; //This works
但是如果使用变量它不起作用:
$sql = "SELECT * FROM chat WHERE name LIKE '%'+$variable+'%' ";
//or
$sql = "SELECT * FROM chat WHERE name LIKE '$variable' ";
我该如何解决?
答案 0 :(得分:3)
在PHP中,您使用点连接字符串。
$sql = "SELECT * FROM chat WHERE name LIKE '" . $variable . "' ";
答案 1 :(得分:0)
您也可以在双引号中使用变量:
$sql = "SELECT * FROM chat WHERE name LIKE '$variable' ";
或
$sql = "SELECT * FROM chat WHERE name LIKE '{$variable}' ";
请记住,双引号比单引号慢。 在你的情况下,它将是
$sql = "SELECT * FROM chat WHERE name LIKE '%{$variable}%' ";