I have a search page where I build a dynamic SQL string using a couple arrays:
$conditions = [];
$parameters = [];
//example - these conditions and parameters will vary
$conditions[] = "training_id = ?";
$parameters[] = $tid;
$conditions[] = "class_date BETWEEN ? AND ?";
$parameters[] = $sd;
$parameters[] = $ed;
I fill the arrays based on search criteria then I build a WHERE out of the values like:
$sql = "SELECT COUNT(*) from viewSingleTraining";
if($conditions)
{
$sql .= " WHERE ".implode(" AND ", $conditions);
}
This builds a nice and clean SQL string.
SELECT COUNT(*) from viewSingleTraining WHERE employee_id = ? AND training_id = ?
I prepare and bind parameters
$stmt = $pdo->prepare($sql);
foreach($parameters as $key => $val)
{
debug_to_console('key and value ' . $key + 1 . ' ' . $val);
$stmt->bindParam($key + 1, $val);
}
$stmt->execute();
debug_to_console shows the correct expected values but I fail on the bindParam function during the attempt at foreach loop.
I get the generic "Fatal error: Uncaught Error: Call to a member function bindParam() on boolean" error which I know means bindParam returned false, but I do not see why.