Please consider using bindValue instead of passing an array. As the saying goes here :
All values ββare processed as PDO :: PARAM_STR.
It should be possible to make this pretty transparent for the rest of your application, since you already have the values ββyou want to UPDATE as an array. Try for example. something like that:
<?php function executeWithDataTypes(PDOStatement $sth, array $values) { $count = 1; foreach($values as $value) { $sth->bindValue($count, $values['value'], $values['type']); $count++; } return $sth->execute(); } $sth = $handle->prepare("UPDATE table SET name = ?, id_extra1 = ?, id_extra2 = ? WHERE id_something = ?"); $values = array(); $values[] = array('value' => 'testing', 'type' => PDO::PARAM_STR); $values[] = array('value' => 2, 'type' => PDO::PARAM_INT); $values[] = array('value' => null, 'type' => PDO::PARAM_NULL); $values[] = array('value' => 1958, 'type' => PDO::PARAM_INT); $result = executeWithDataTypes($sth, $values); ?>
As you noted that using bindParam has given you headaches in the past, pay attention to the subtle difference between bindValue and bindParam . Personally, I never use bindParam because of side effects that make scripts difficult to understand, although, of course, there are times when these effects will be useful.
EDIT: you could, of course, simplify the function even more and get rid of the need to specify the type as an additional key in the passed array, doing something like:
$type = PDO::PARAM_STR; switch(true) { case is_null($value): $type = PDO::PARAM_NULL; break; case is_numeric($value): $type = PDO::PARAM_INT; break;
and determine the type based on the type of value passed in the array; however, which is more error prone since, for example, floats are also numeric, and this will lead to an incorrect solution in the above switch statement, but I thought I mentioned this for the sake of completeness.