php / pecl-database-oci8

PHP extension to interact with Oracle Database
https://developer.oracle.com/languages/php.html
Other
6 stars 4 forks source link

Number columns are being converted to strings when using oci8 #5

Open wmiguellima opened 1 year ago

wmiguellima commented 1 year ago

Description

The following code:

$conn = oci_connect('DB', 'PASSWORD', '172.17.0.1:51521/XEPDB1');

if (!$conn) {
    $e = oci_error();
    trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
}

$stid = oci_parse($conn, 'SELECT * FROM table');
oci_execute($stid);

while ($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) {
    foreach ($row as $item) {
      echo gettype($item) . ' | ';
    }
}

Resulted in this output:

string | object | string |

But I expected this output instead:

integer | object | integer |

Problem:

The integer columns are being converted to strings.

Using CAST is not an option.

Possible solution:

Something like PDO::ATTR_STRINGIFY_FETCHES available on de sqlserver pdo

PHP Version

PHP 8.1

Operating System

Ubuntu 20.04

cmb69 commented 1 year ago

While the documentation doesn't appear to clarify this, I don't think that returning strings for numeric column values qualifies as bug. E.g. there is fetch.phpt which actually expects strings in this case. Furthermore, changing the behavior in stable versions might cause more harm than good (besides that values which would exceed PHP's int range would still need to be returned as strings).

So I'm changing this to feature request, although transferring to doc-en might be the more sensible thing to do.

flavio-alves-roboyo commented 1 year ago

Just to give more context, we have an application that works flawlessly with MySQL/MariaDB/SQL Server, thanks to Laravel Eloquent ORM and no usage of specific syntax. We are trying to add support to Oracle (21c) and right now this is the only thing that is preventing us to do that. The code base is huge, so it is unfeasible to cast the applicable values.

What about create an option, like @wmiguellima proposes, to opt-in for number columns as int (sure, as long as the value doesn't exceeed PHP's int range)?