-1

I have a variable $Search and i want this query

$sel=$con->prepare("SELECT * FROM users WHERE Username LIKE '%{:src}%'");
$sel->bindValue(":src",$Search);
$sel->execute();

I wanted to know if it's safe for me to do this query without doing any filtration on user's input.

sasasasa
  • 137
  • 2
  • 8
  • 1
    That won't work as it will end up as `'%{':src'}%'` – AbraCadaver Jun 02 '17 at 16:46
  • it should be `$sel->bindValue(":src",'%'.$Search.'%');`. and your query should be `$sel=$con->prepare("SELECT * FROM users WHERE Username LIKE :src");` And yes, it will be safe. – Dimi Jun 02 '17 at 16:47
  • Or you can do `LIKE CONCAT('%', :src, '%')` and bind it like you are. – Qirel Jun 02 '17 at 16:54

1 Answers1

0

Prepared statements quote your data for you, but for your query to work you need to do it this way:

$Search = "%$Search%";
$sel = $con->prepare("SELECT * FROM users WHERE Username LIKE :src");
$sel->bindValue(":src", $Search, PDO::PARAM_STR);

Or directly:

$sel = $con->prepare("SELECT * FROM users WHERE Username LIKE :src");
$sel->bindValue(":src", "%$Search%", PDO::PARAM_STR);

If you use bindParam() you need to use the first option as it needs a variable as a reference.

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87