2

I have a problem on SQL query which contains bindValue. The error: Fatal error: Uncaught Error: Call to a member function bindValue() on bool (240).

But I can't see any problem in the codes. Help pls :/

$count = $db->query("SELECT * FROM songs WHERE title LIKE :search");
$count->bindValue(":search","%{$search}%",PDO::PARAM_STR); // (line:240)
$count->execute();
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • 1
    `PDO::query()` returns a PDOStatement object, or FALSE on failure. You should be using `PDO::prepare()` instead if you're using `bindValue` – zanderwar Jun 01 '19 at 11:31

1 Answers1

4

In order to use the function bindValue() you want to work with a prepared statement. So instead of calling query(), use prepare().

$count = $db->prepare("SELECT * FROM songs WHERE title LIKE :search");
$count->bindValue(":search","%{$search}%",PDO::PARAM_STR);
$count->execute();
diegowc
  • 455
  • 2
  • 14