ResultSet always returns string (Pgsql adapter)

Hello everyone,

I started to use zend-db to query a PostgreSQL database ; it obviously works fine but I’m a bit confused with the ResultSet interface.
No matter what datatype my columns are in the database, it always returns a “string”.

Here is an example of my query :

$sql = new Sql($adapter, $config['postgresql']['table']);

$select = $sql->select();
$select->columns([
    'status',
    'count' => new Expression('COUNT(*)'),
]);
$select->group([
    'status',
]);

$qsz = $sql->buildSqlString($select);
$statitics = $adapter->query($qsz, $adapter::QUERY_MODE_EXECUTE);

No matter what I do, I receive status and count as string but they are both integer !

I guess I missed something and would need some help to figure out how to receive it with the correct datatype.

Thanks a lot :slight_smile:

Hello @jbelien,

try configuring the pdo adapter.
Set PDO::ATTR_STRINGIFY_FETCHES to FALSE.

e.g. for sqlite:

return [
    'db' => [
        'driver' => 'Pdo',
        'dsn'    => 'sqlite:./database.sqlite',
        'driver_options' => [
            PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\'',
            PDO::ATTR_STRINGIFY_FETCHES  => FALSE,
        ],
    ],
];
1 Like

Hello @lowtower,

I was using Pgsql adapter ; I switched to Pdo_Pgsql adapter and the “issue” is fixed.
So I guess, there is an option I didn’t find for Pgsql adapter (or maybe a bug with that adapter).

I’ll keep using Pdo_Pgsql adapter and see if everything works as excepted :slight_smile:

Thanks a lot for the tip !