0

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.

Rimšiakas
  • 11
  • 1
  • 2

1 Answers1

-2

Try this

$_SESSION["searchfield"] = $_POST["searchfield"];
$_SESSION["searchtype"] = $_POST["searchtype"];

//connection comes from an included file.
$query=$connection->prepare("SELECT COUNT(*) AS count FROM vendor WHERE ".$_SESSION["searchtype"]." = :searchcriteria");
$query->bindValue(":searchcriteria", $_SESSION["searchfield"]);
$query->execute();
$countid=$query->fetch(PDO::FETCH_ASSOC);

Edit: the change is bold as somebody is asking where is the change $query=$connection->prepare("SELECT COUNT(*) AS count FROM vendor WHERE ".$_SESSION["searchtype"]." = :searchcriteria");

laurent
  • 418
  • 3
  • 7
  • 3
    Try _what_?? Please always include a good explanation of what you changed and why it makes a difference. – Michael Berkowski May 12 '14 at 14:03
  • Still doesn't work. The $query->errorInfo() doesn't show any error, but there still are no result even though I am SURE that there are entries in the database that match. – Rimšiakas May 12 '14 at 14:08