I’m trying to make a nice little function to do my PDO binding for me:
function autoBind($result, $values)
{
$i = 1 ;
foreach($values as $currentValue) {
if(is_numeric($currentValue))
{
$bindType = PDO::PARAM_INT ;
}
elseif(is_string($currentValue) || is_float($currentValue))
{
$bindType = PDO::PARAM_STR ;
}
else
{
return false ; // Error
}
$result->bindParam($i, $currentValue, $bindType) ;
$i++ ;
}
$result->execute() ;
}
But it’s not working. If I echo $bindType, it comes out as “1” and then “2”.
What do I need to do to make it give me what I want?
Thanks for your replies,
Is this as safe as manually binding?
In what situation would you manually bind?
There is no need for this function, as PDOStatement::execute already supports passing in an array of parameters. Simply do: