85

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?

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
dmontain
  • 1,621
  • 4
  • 14
  • 16

4 Answers4

106

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

Alix Axel
  • 151,645
  • 95
  • 393
  • 500
  • 2
    That was one of the things I tried and didn't work before posting the question here. – dmontain Apr 27 '10 at 04:33
  • @dmontain: Try defining $decval as a string (eg.: `'1.0'` instead of `1.0`) or use `strval($decval)`. – Alix Axel Apr 27 '10 at 04:56
  • 6
    @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 Apr 27 '10 at 06:32
  • @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 Apr 27 '10 at 07:04
  • 19
    @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 Apr 27 '10 at 07:13
0

UP must use the PDO::PARAM_STR same, and noting that if the column in the database is of type NUMERIC (M, D) or decimal (M, D) should be observed that the M is the number of characters precision should be the total of characters that you can enter, and D the number of decimal places. In the example NUMERIC (10,2) we have 8 houses to places of accuracy and two decimal places. So we have 10 characters in total with 2 reserved for decimal places.

Sam
  • 7,252
  • 16
  • 46
  • 65
Gil
  • 11
  • 1
0

I found a solution, send the decimal parameter like PDO::PARAM_STR, in the sql server store procedure receive it like decimal(int,dec)

PHP

$stmt_e->bindParam(':valor_escala_desde', $valor_escala_desde, PDO::PARAM_STR);

SQL SERVER, STORE PROCEDURE:

@valor_escala_desde decimal(18,2),

and is done.

0

use PDO::PARAM_STR for all column types which are not of type int or Bool

Paul Kiarie
  • 37
  • 1
  • 8