MySQL Query with Variable Wildcards

If I execute my PHP code:

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

I get a split error, how to use a variable in a query with wildcards on both sides?

+4
source share
2 answers

correct request

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

And you must use prepared statements

+3
source
 $sql = "SELECT NAME FROM PLAYERS WHERE NAME LIKE '%" .$serName. "%'"; 
0
source

All Articles