带有可变通配符的MySQL Query

时间:2013-04-07 16:09:16

标签: php mysql wildcard

如果我执行我的PHP代码:

$serName = $_GET['username'];
// Code for sanitation here
// [...]
$sql = "SELECT NAME FROM PLAYERS WHERE NAME LIKE '%$serName%'"; 

我得到除法错误,如何在双方使用通配符的查询中使用变量?

2 个答案:

答案 0 :(得分:3)

正确的查询

SELECT NAME FROM PLAYERS WHERE NAME LIKE '%{$serName}%'

你应该使用prepared statements

答案 1 :(得分:0)

$sql = "SELECT NAME FROM PLAYERS WHERE NAME LIKE '%" .$serName. "%'";