2

Using PHP PDO with this SQL statement

SELECT count(*) FROM my_table;

returns an INTEGER with Postgres and a STRING with Sqlite3. That is, if there is one row in the table, Postgres returns (int)1 and Sqlite returns '1'.

Is this as intended, or is this a bug?

[edit to add below]

In case you want to follow along at home, here's a demonstration script I threw together. I actually encountered this when my PHPUnit tests passed (using in-memory Sqlite as a test fixture) and but my application failed using the production Postrgres database.

<?php
function connect($dsn)
{
    try {
        $pdo = new \PDO($dsn);
    }
    catch (\PDOException $e) {
        echo 'New PDO failed: ' . $e->getMessage() . PHP_EOL;
        exit;
    }
    return $pdo;
}

function doQuery($pdo, $sql)
{
    if ( ($result = $pdo->query($sql)) === false) {
        echo "'$sql' failed: " . print_r($pdo->errorInfo(), true) . PHP_EOL;
    }
    return $result;
}

$pgo = connect('pgsql:host=localhost;dbname=postgres');
$sqo = connect('sqlite::memory:');

doQuery($pgo, 'DROP TABLE IF EXISTS public.foo');
doQuery($pgo, 'CREATE TABLE public.foo ( ii int )');
doQuery($pgo, 'INSERT INTO public.foo VALUES (42)');

doQuery($sqo, "ATTACH DATABASE ':memory:' AS public;") or die();
doQuery($sqo, 'DROP TABLE IF EXISTS public.foo');
doQuery($sqo, 'CREATE TABLE public.foo ( ii int )');
doQuery($sqo, 'INSERT INTO public.foo VALUES (42)');

$pgResult = doQuery($pgo, 'SELECT COUNT(ii) FROM foo');
echo 'Postgres: ';
var_dump($pgResult->fetchColumn());

echo 'Sqlite3: ';
$ltResult = doQuery($sqo, 'SELECT COUNT(ii) FROM foo');
var_dump($ltResult->fetchColumn());

2 Answers 2

1

This is a side effect of sqlite not having datatypes. Or rather, having what they call the dynamic type system. But quite interestingly

SELECT TYPEOF(b) FROM ( select count(*) as b from my_table) a;

produces integer as the output! So clearly something is being lost in translation from sqlite to php. However it doesn't really matter because in php '1' + 2 gives 3. Because let's not forget, PHP is also a dynamic typed system.

Sign up to request clarification or add additional context in comments.

6 Comments

I really hope so. I am horrible at arithmatic. @muistooshort
<?php var_dump('2' + 1); var_dump(2 + '1'); int(3) int(3)
@CXJ I was just kidding in my previous comment. I had made a typo in the answer
@e4c5 Yup. But sometimes PHP's dynamic typing produces unexpected results, so I thought I'd explicitly test it.
@CXJ Implicit conversion between strings and integers (and other data types) is not dynamic typing. Some people call it weak typing, but that term is fairly ill-defined.
|
0

Reported and accepted as a PHP PDO bug:

https://bugs.php.net/bug.php?id=72798

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.