php / pecl-database-ibm_db2

Extension for IBM DB2 Universal Database, IBM Cloudscape, and Apache Derby
http://pecl.php.net/package/ibm_db2
Apache License 2.0
23 stars 27 forks source link

Field return types using db2_fetch_assoc #27

Closed DannyRidgway closed 2 years ago

DannyRidgway commented 2 years ago

Just a small question regarding expected functionality. I could not find it in the docs but should the values returned from db2_fetch_assoc match the same types as the table/field definition?

E.g. ITEMID = VARCHAR 68 ITEMSEQN = DECIMAL 5

In the associative array I would expect the ITEMID to be a string which it is however the ITEMSEQN field is also returned as a string type.

Is this how it's meant to work? I would have expected numerical fields to be returned as a int or double in PHP.

If so do you know of a way to force the types returned from db2_fetch_assoc to be the correct type?

I could manually correct it in our project however this function is used around 1000 times and the effort required would be enormous.

NattyNarwhal commented 2 years ago

Decimal/numeric are treated as strings because they don't quite map to a C/PHP type without accuracy loss. While a five-digit decimal without mantissa could fit as integer/float, it'd be harder for say, a decimal(10, 2) - you'd lose the decimal information as an int, and floating point isn't accurate enough for a non-lossy conversion.

I'd say cast it - you imply PHP level would be too much effort, but what about at the SQL level? An SQL integer/float will have the same representation.

DannyRidgway commented 2 years ago

Thanks