3

I'm testing a small search feature:

But I've come across an error that I cannot seem to solve. You can see the PDO query here:

$search = "test1"; //later to be changes to $_POST ['search'];

$sql = "SELECT id, name FROM clients WHEE name like %:name% order by id LIMIT 5";
$stm = $db->prepare ( $sql );
$stm->bindParam ( ":name" , $search);
$result = $stm->execute ();

As you can see, I'm trying to bind the parameter %:name% from my query, but I don't know if that's actually possible?

I receive the error:

Uncaught exception 'PDOException' with message 'SQLSTATE[42000]:.....

And I can see in the error that '' has been put around test1 %'test1'%

Is what I'm trying possible, or do I need to do something like this?

$query = "SELECT id, name FROM clients WHEE name like :name order by id LIMIT 5";

$sql->execute(array(":name" => "%" .$search . "%"));
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • This question already have an answer checkout the URL. http://stackoverflow.com/questions/11068230/using-like-in-bindparam-for-a-mysql-pdo-query – jamseernj Nov 20 '14 at 23:22
  • 2
    You've misspelled `WHERE` as `WHEE`. –  Nov 20 '14 at 23:23
  • I rolled back your later edits as they fixed the issue that the comment and answer helped you to solve. – Dharman Oct 15 '19 at 22:29

1 Answers1

6

Use

LIKE CONCAT('%', :name, '%')
Mihai
  • 26,325
  • 7
  • 66
  • 81