I am trying to write a helper query function to return a resultset to the caller, but when the caller provides a prepared statement using named params, I am not sure how to bind them in a function.
function queryDB(string $query, array $param=null) {
global $dbh; //reference the db handle declared in init.php
if (isset($param)) { //query params provided, so a prepared statement
$stmt = $dbh->prepare($query);
for($i = 1; $i <= count($param);$i++) { //bind the parameters 1-by-1
$stmt->bindParam($i, $param[$i]); //
}
$stmt->execute();
} else { //a straight sql query, not a prepared statement
$stmt = $dbh->query($query);
}
$result = $stmt->fetchAll();
return $result;
}
If I call queryDB($query, [$name, $age])
with an unnamed-param prepared statement such as $query = INSERT INTO users (name, age) VALUES(?, ?)
and $name = "trump"; $age = 18
, that code should work.
But there can be times when I (or someone else) will call with a named-param prepared statement such as $query = INSERT INTO users (name, age) VALUES(:name, :age)
and $name = "trump"; $age = 18
. The existing bindParam(i, $value) shouldn't work but then the function wouldn't know those :name, :age, :whatever
named parameters. How should I write the bindParam(param, value)
to accommodate both named and unnamed prepared statements? assuming params will be provided in the matched order even when named.