Bug #72798 [Ana]: SELECT COUNT() returns string, not int.

From: Date: Wed, 10 Aug 2016 10:15:00 +0000
Subject: Bug #72798 [Ana]: SELECT COUNT() returns string, not int.
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-203140@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=72798&edit=1 ID: 72798 Updated by: cmb@php.net Reported by: php dot chaska at xoxy dot net Summary: SELECT COUNT() returns string, not int. Status: Analyzed Type: Bug Package: PDO SQLite Operating System: FreeBSD, Mac OSX PHP Version: 5.6.24 Block user comment: N Private report: N New Comment: On x86 builds (which are still distributed), however: PHP_INT_MAX === 2147483647 Previous Comments: ------------------------------------------------------------------------ [2016-08-10 09:59:23] kalle@php.net @yohgaki well in 7.0+, we have a much better and more reliant 64 bit system implemented, and I think that at least on Windows we would make PHP's int always 64 bit, and have a way to emulate that, and I don't see a reason why we cannot reliably convert to a PHP int if we can guarantee that PHP's int always will be at mimimum 64 bit. As for the 128 bit part, I expect us to support 128 bit as other database systems and hardware decides to adopt that, so I don't forsee that as an issue. ------------------------------------------------------------------------ [2016-08-10 09:51:09] cmb@php.net > Automatic conversion is OK only when external numeric value and > PHP type is 100% compatible. Therefore, conversion must not be > automatic, but manual. Otherwise, programs lose data or > misbehave. IMHO, it is better to do the conversion internally, if and only if it is lossless (like sqlite_value_to_zval() does). Otherwise the burden of proper conversion is up to the userland developer, who might easily just do a simple (int) $val, what would be wrong, see <https://3v4l.org/jgO2M>. With ext/sqlite3 the dev could rely on a simple is_int($val) instead of `$val <= PHP_INT_MAX && $val >= PHP_INT_MIN or filter_var($val, FILTER_VALIDATE_INT) !== false`. Anyhow, the behavior of the different PDO drivers should be adjusted to match each other. Doing otherwise, would somehow defeat the the purpose of having PDO[1] (emphasis mine): > The PHP Data Objects (PDO) extension defines a lightweight, > *consistent* interface for accessing databases in PHP. [1] <http://php.net/manual/en/intro.pdo.php> ------------------------------------------------------------------------ [2016-08-09 21:40:27] yohgaki@php.net External numeric values must not be converted to PHP (or any other language) types automatically. Automatic conversion is OK only when external numeric value and PHP type is 100% compatible. Therefore, conversion must not be automatic, but manual. Otherwise, programs lose data or misbehave. We've seen this kind of problems in DB, XML, JSON, etc already and should not create no more problems. ------------------------------------------------------------------------ [2016-08-09 21:27:31] yohgaki@php.net Numeric values MUST NOT be converted to PHP types. It just don't work. Int could be 32 or 64 bits (+ signedness). It may be 32, 64 or 128 bits in near future. ------------------------------------------------------------------------ [2016-08-09 20:00:56] cmb@php.net I can confirm this behavior for other queries as well, see <https://3v4l.org/bh03r>. Actually, this report is a duplicate of request #38334, but I do not really agree with the mentioned reasoning that "SQLite by its nature is a typeless database, […]". SQLite3 supports manifest typing[1], what is not typeless; otherwise one may claim that PHP would be also typeless. And, for what it's worth, ext/sqlite3 returns an int from the same query, see <https://3v4l.org/hiPXa#v560>. It shouldn't be too hard to add something like sqlite_value_to_zval() to PDO_SQLite. [1] <http://sqlite.org/different.html#typing> [2] <https://github.com/php/php-src/blob/PHP-7.0.10/ext/sqlite3/sqlite3.c#L580-L608> ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at https://bugs.php.net/bug.php?id=72798 -- Edit this bug report at https://bugs.php.net/bug.php?id=72798&edit=1

« previous php.bugs (#203140) next »