I am having some problems with running my PDO query. I am trying to get the number of database entries with matching criteria from a search form. The query returns nothing. $query->errorInfo() prints a HY093 error. The session variables are indeed set, I've checked, and there definitely are entries in the database that match the session values.
$_SESSION["searchfield"] = $_POST["searchfield"];
$_SESSION["searchtype"] = $_POST["searchtype"];
//connection comes from an included file.
$query=$connection->prepare("SELECT COUNT(*) AS count FROM vendor WHERE :searchby = :searchcriteria");
$query->bindValue(":searchby", $_SESSION["searchtype"]);
$query->bindValue(":searchcriteria", $_SESSION["searchfield"]);
$query->execute();
$countid=$query->fetch(PDO::FETCH_ASSOC);
I just can't find anything wrong in my syntax. It works well without filtering:
$query=$connection->query("SELECT COUNT(*) AS count FROM vendor");
$countid=$query->fetch(PDO::FETCH_ASSOC);
Any help would be much appreciated.
EDIT
@GolezTrol, seems like that may be the problem, thanks.