Search code examples
phpmysqlsqlselectsql-like

Using SQL LIKE with variable PHP


I can't use the SQL LIKE with a variable

$sql = "SELECT * FROM chat WHERE name LIKE 'Motherboard' "; //This works

But if a use a variable it doesn't work:

$sql = "SELECT * FROM chat WHERE name LIKE '%'+$variable+'%' ";
//or
$sql = "SELECT * FROM chat WHERE name LIKE '$variable' ";

How can I fix it?


Solution

  • In PHP you concatenate strings with a dot .

     $sql = "SELECT * FROM chat WHERE name LIKE '" . $variable . "' ";