1

I'm trying to get PDO to return the results of a wildcard search. My code is:

$search = "%Notes%"; 
$result = $db->prepare("SELECT * FROM books WHERE 'name' LIKE :search");
$result->bindParam(':search', $search);

$result->execute();

while($arr = $result->fetch(PDO::FETCH_ASSOC)){
        echo $arr['name'];
}

At the moment, I get a blank screen. If I run the sequel through PHPMyAdmin:

SELECT * FROM books WHERE name LIKE '%Notes%'

I get the appropriate result.

I assume it's something to do with the way I am formatting my PDO statement, I know you can't have a dynamic column name but I don't see what is going wrong?

CrabLab
  • 182
  • 2
  • 17

1 Answers1

2

in your query you have 'name' change that to just backticks instead of quotes

aka

$result = $db->prepare("SELECT * FROM `books` WHERE `name` LIKE :search");

you can also just remove the backticks

John Ruddell
  • 25,283
  • 6
  • 57
  • 86