|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2019-08-16 00:57 UTC] ludwigdiehl at gmail dot com
Description:
------------
If you create a prepared statement from a non-results query, you get the following return values after calling fetchAll and fetch respectively:
fetchAll: EMPTY ARRAY. Which is the desired return value.
fetch: FALSE. Should it not be NULL?
According to the documentation, "In all cases, FALSE is returned on failure but this is not an error isn't it?.
Test script:
---------------
$pdo = new PDO('mysql:host=x.x.x.x;dbname=xxx','user','password');
$stmt = $pdo->prepare('SELECT * FROM mytable WHERE 1=12');
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$stmt->execute();
$result = $stmt->fetch();
var_dump($result);
$pdo = new PDO('mysql:host=x.x.x.x;dbname=xxx','user','password');
$stmt = $pdo->prepare('SELECT * FROM mytable WHERE 1=12');
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$stmt->execute();
$result = $stmt->fetchAll();
var_dump($result);
Expected result:
----------------
I think NULL should be return value of the fetch method instead of FALSE for an empty result.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 02:00:01 2025 UTC |
Thanks for your reply. I have an example which produces same results even if there is an error. /*DATABASE*/ /*Table structure for table `tbl_test` */ DROP TABLE IF EXISTS `tbl_test`; CREATE TABLE `tbl_test` ( `id_test` int(10) unsigned NOT NULL AUTO_INCREMENT, `test` varchar(80) COLLATE utf8_unicode_ci DEFAULT NULL, `created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id_test`) ) ENGINE=InnoDB AUTO_INCREMENT=43 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; /*Data for the table `tbl_test` */ insert into `tbl_test`(`id_test`,`test`,`created`) values (1,'3','2019-08-20 11:38:19'),(2,'1','2019-08-15 15:08:17'),(3,'1','2019-08-15 15:08:18'),(4,'1','2019-08-15 15:08:18'); /*SAMPLE CODE*/ try { $host = 'x.x.x.x'; $dbname = 'dbname'; $user = 'user'; $pwd = 'pwd'; $charset = 'utf8'; $pdo = new PDO( sprintf('mysql:host=%s;dbname=%s;charset=%s', $host,$dbname,$charset), $user,$pwd,[ //PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, ]); $result = null; $queries = [ 'SELECT * FROM tbl_test WHERE 1=0', //EMPTY RESULTSET 'SELECT id_test,test FROM tbl_test WHERE 1=1', //GOOD QUERY 'SELECT * FROM tbl_test WHERE 1=1', //RESULTSET MUST HAVE 2 COLUMNS 'SELECT * FROM tbl_test WHERE' //WRONG QUERY ]; foreach($queries as $query) { $stmt = $pdo->prepare($query); $stmt->execute(); $result = $stmt->fetch(PDO::FETCH_KEY_PAIR); echo 'Result for query '.$query.' : '.json_encode($result)."\n"; } } catch(PDOException $e) { echo $e->getMessage(); } It will produce these results (without PDO::ERRMODE_EXCEPTION): Result for query SELECT * FROM tbl_test WHERE 1=0 : false Result for query SELECT id_test,test FROM tbl_test WHERE 1=1 : {"1":"3"} <br /> <b>Warning</b>: PDOStatement::fetch(): SQLSTATE[HY000]: General error: PDO::FETCH_KEY_PAIR fetch mode requires the result set to contain extactly 2 columns. in <b>/var/www/pruebas/ldiehl/2019/Database/index.php</b> on line <b>34</b><br /> Result for query SELECT * FROM tbl_test WHERE 1=1 : false Result for query SELECT * FROM tbl_test WHERE : false And these results (with PDO::ERRMODE_EXCEPTION): Result for query SELECT * FROM tbl_test WHERE 1=0 : false Result for query SELECT id_test,test FROM tbl_test WHERE 1=1 : {"1":"3"} SQLSTATE[HY000]: General error: PDO::FETCH_KEY_PAIR fetch mode requires the result set to contain extactly 2 columns. If somebody is not working with exceptions both the empty and the error will produce the same result is it not ambiguous?