views:

387

answers:

3

I have 2 database fields

`decval` decimal(5,2)
`intval` int(3)

I have 2 pdo queries that update them. The one that updates the int works ok

$update_intval->bindParam(':intval', $intval, PDO::PARAM_INT);

but I can't update the decimal field. I've tried the 3 ways below, but nothing works

$update_decval->bindParam(':decval', $decval, PDO::PARAM_STR);
$update_decval->bindParam(':decval', $decval, PDO::PARAM_INT);
$update_decval->bindParam(':decval', $decval);

It seems the problem is with the database type decimal? Is there a PDO::PARAM for a field of type decimal? If not, what do I use as a workaround?

A: 

use dot instead of comma for the separator?

Col. Shrapnel
The comma is for the MySQL `DECIMAL` data type definition (see http://dev.mysql.com/doc/refman/5.1/en/precision-math-decimal-changes.html).
Alix Axel
+1  A: 

There isn't any PDO::PARAM for decimals / floats, you'll have to use PDO::PARAM_STR.

Alix Axel
That was one of the things I tried and didn't work before posting the question here.
dmontain
@dmontain: Try defining $decval as a string (eg.: `'1.0'` instead of `1.0`) or use `strval($decval)`.
Alix Axel
@dmontain: It should work out of the box (`PDO::PARAM_STR` is the default when none is specified), and **you shouldn't** change your column data type. Just enclose the value with single or double quotes or `strval()` it, like this: `$update_decval->bindParam(':decval', strval($decval), PDO::PARAM_STR);`.
Alix Axel
@Alix, I have no idea why `strval` isn't working. It sounds like a very logical answer, but something is not working there. I'm trying different combinations to see if any of them would work.
dmontain
@dmontain: Try rebooting your computer. =) Just kidding, throw it out the window instead! xD
Alix Axel
@dmontain: From the PHP Manual: **Unlike PDOStatement::bindValue(), *the variable is bound as a reference* and will only be evaluated at the time that PDOStatement::execute() is called.** - you have to use `bindValue()` instead if you want to use `strval()`.
Alix Axel
A: 

I found out, looking for a way of storing DOULBEs using PDO, that if you don't provide the third argument (i.e. PDO::PARAM_*) it uses some kind of magic and stores the number correctly. So maybe if you just left PDO to worry about the parameter type and merrily bind :decval to a $decval and hop along to the next task :-))

Peter Perháč