Question

I am trying out PDO in PHP for the first time. I thought that PDOStatement->bindParam() would be a cool way to set the datatypes of the values that i pass in to the sql query. But somehow it doesnt work for me. For example I set the type of a variable to INT in bindParam call. But it still doesnt throw error even when I pass pure string values to it. Maybe I am doing something wrong. Here is the snippet of the code..

$query = "select * from PDO_TABLE where A_COLUMN = :test1 or B_COLUMN = :test2";

$test1 = '0';
$test2 = 'a';
$preparedStatement = $conn->prepare($query);

echo $preparedStatement->bindParam(':test1', $test1, PDO::PARAM_INT);
echo $preparedStatement->bindParam(':test2', $test2, PDO::PARAM_INT);

$preparedStatement->execute();

Am I doing everything properly? Isnt this supposed to throw error for the parameter test2?

Was it helpful?

Solution

PDO won't throw an exception or raise an error but at best convert the parameter to an integer/long. E.g. in pdo_stmt.c:

if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_INT && Z_TYPE_P(param->parameter) == IS_BOOL) {
  convert_to_long(param->parameter);
}

i.e. if you have registered a parameter as PDO_INT but the variable holds a boolean PDO converts the bool to an int/long.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top