]> granicus.if.org Git - php/commitdiff
MFB: Fixed bug #42917 (PDO::FETCH_KEY_PAIR doesn't work with setFetchMode)
authorIlia Alshanetsky <iliaa@php.net>
Thu, 11 Oct 2007 20:56:22 +0000 (20:56 +0000)
committerIlia Alshanetsky <iliaa@php.net>
Thu, 11 Oct 2007 20:56:22 +0000 (20:56 +0000)
NEWS
ext/pdo/pdo_stmt.c
ext/pdo/tests/bug_42917.phpt [new file with mode: 0644]

diff --git a/NEWS b/NEWS
index eae07f36de2c4855b69ef035ec112514e1831773..d4cd25cd1a0bc7f97508d7ae44f605565208c29a 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -32,6 +32,8 @@ PHP                                                                        NEWS
 - Fixed htmlentities/htmlspecialchars not to accept partial multibyte
   sequences. (Stas)
 
+- Fixed bug #42917 (PDO::FETCH_KEY_PAIR doesn't work with setFetchMode).
+  (Ilia)
 - Fixed bug #42890 (Constant "LIST" defined by mysqlclient and c-client).
   (Andrey)
 - Fixed bug #42869 (automatic session id insertion adds sessions id to
index ef86c7e1acbb945c979d78e4e2eca9c55db45198..7b9482098676b146f828cd7ca4216c618560c59e 100755 (executable)
@@ -1260,7 +1260,7 @@ static int do_fetch(pdo_stmt_t *stmt, int do_bind, zval *return_value,
 }
 /* }}} */
 
-static int pdo_stmt_verify_mode(pdo_stmt_t *stmt, int mode, int fetch_all TSRMLS_DC) /* {{{ */
+static int pdo_stmt_verify_mode(pdo_stmt_t *stmt, long mode, int fetch_all TSRMLS_DC) /* {{{ */
 {
        int flags = mode & PDO_FETCH_FLAGS;
 
@@ -1549,7 +1549,9 @@ static PHP_METHOD(PDOStatement, fetchAll)
        if (!error)     {
                PDO_STMT_CLEAR_ERR();
                MAKE_STD_ZVAL(data);
-               if ((how & PDO_FETCH_GROUP) || how == PDO_FETCH_KEY_PAIR) {
+               if (    (how & PDO_FETCH_GROUP) || how == PDO_FETCH_KEY_PAIR || 
+                       (how == PDO_FETCH_USE_DEFAULT && stmt->default_fetch_type == PDO_FETCH_KEY_PAIR)
+               ) {
                        array_init(return_value);
                        return_all = return_value;
                } else {
@@ -1565,7 +1567,7 @@ static PHP_METHOD(PDOStatement, fetchAll)
                        do {
                                MAKE_STD_ZVAL(data);
                        } while (do_fetch(stmt, TRUE, data, how, PDO_FETCH_ORI_NEXT, 0, return_all TSRMLS_CC));
-               } else if (how == PDO_FETCH_KEY_PAIR) {
+               } else if (how == PDO_FETCH_KEY_PAIR || (how == PDO_FETCH_USE_DEFAULT && stmt->default_fetch_type == PDO_FETCH_KEY_PAIR)) {
                        while (do_fetch(stmt, TRUE, data, how, PDO_FETCH_ORI_NEXT, 0, return_all TSRMLS_CC));
                } else {
                        array_init(return_value);
@@ -1916,6 +1918,7 @@ fail_out:
                case PDO_FETCH_OBJ:
                case PDO_FETCH_BOUND:
                case PDO_FETCH_NAMED:
+               case PDO_FETCH_KEY_PAIR:
                        break;
 
                case PDO_FETCH_COLUMN:
diff --git a/ext/pdo/tests/bug_42917.phpt b/ext/pdo/tests/bug_42917.phpt
new file mode 100644 (file)
index 0000000..d4b71f5
--- /dev/null
@@ -0,0 +1,40 @@
+--TEST--
+PDO Common: Bug #42917 (PDO::FETCH_KEY_PAIR doesn't work with setFetchMode)
+--SKIPIF--
+<?php # vim:ft=php
+if (!extension_loaded('pdo')) die('skip');
+$dir = getenv('REDIR_TEST_DIR');
+if (false == $dir) die('skip no driver');
+require_once $dir . 'pdo_test.inc';
+PDOTest::skip();
+?>
+--FILE--
+<?php
+if (getenv('REDIR_TEST_DIR') === false) putenv('REDIR_TEST_DIR='.dirname(__FILE__) . '/../../pdo/tests/');
+require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc';
+$db = PDOTest::factory();
+
+$db->exec("CREATE TABLE test (a varchar(100), b varchar(100), c varchar(100))");
+
+for ($i = 0; $i < 5; $i++) {
+       $db->exec("INSERT INTO test (a,b,c) VALUES('test".$i."','".$i."','".$i."')");
+}
+
+$res = $db->query("SELECT a,b FROM test");
+$res->setFetchMode(PDO::FETCH_KEY_PAIR);
+var_dump($res->fetchAll());
+
+?>
+--EXPECT--
+array(5) {
+  ["test0"]=>
+  string(1) "0"
+  ["test1"]=>
+  string(1) "1"
+  ["test2"]=>
+  string(1) "2"
+  ["test3"]=>
+  string(1) "3"
+  ["test4"]=>
+  string(1) "4"
+}