Luke Pittman
Luke Pittman

Reputation: 843

PDO - Dynamic bindParam - value of 0 strange result

So thanks to suggestions from users here I have started porting my code over to PDO. Everything was going well until one little issue.

I have a small function to handle my database calls, which basically generates the SQL query, does the $dbh->prepare ($sql), then loops through and binds the values, then executes the query.

$sth = $dbh->prepare ($sql);
// bind parameters
if ($action == 'insert' || $action == 'update') {
    reset ($array);
    foreach ($array as $key => &$value) {
        if ($value != 'NOW()') {
            $sth->bindParam (':' . $key, $value);
        }
    }
}
$sth->execute();

This works fine until I need to insert a value of '0'. No errors are returned, but the value that is inserted into the db turns out to be the maximum value for the column type in the table, in this case is "137".

I'd prefer if someone could explain what is happening as well as providing a solution rather than just give me a fix so I can better understand this.

Cheers, Luke

Upvotes: 0

Views: 1037

Answers (1)

Nir Alfasi
Nir Alfasi

Reputation: 53545

You're not binding the parameters correctly, check out the manual.
You should use:

$sth->bindParam (':' . $value, [PARAM TYPE - example: PDO::PARAM_INT]);

Upvotes: 1

Related Questions