PDO::PARAM for type decimal?

PhpMysqlDatabasePdo

Php Problem Overview


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?

Php Solutions


Solution 1 - Php

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

Solution 2 - Php

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.

Solution 3 - Php

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.

Solution 4 - Php

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondmontainView Question on Stackoverflow
Solution 1 - PhpAlix AxelView Answer on Stackoverflow
Solution 2 - PhpGilView Answer on Stackoverflow
Solution 3 - Phpjulio castilloView Answer on Stackoverflow
Solution 4 - PhpPaul KiarieView Answer on Stackoverflow