Annotation of embedaddon/php/ext/mysqli/tests/mysqli_stmt_affected_rows.phpt, revision 1.1

1.1     ! misho       1: --TEST--
        !             2: mysqli_stmt_affected_rows()
        !             3: --SKIPIF--
        !             4: <?php
        !             5: require_once('skipif.inc');
        !             6: require_once('skipifemb.inc');
        !             7: require_once('skipifconnectfailure.inc');
        !             8: ?>
        !             9: --FILE--
        !            10: <?php
        !            11:        require_once("connect.inc");
        !            12: 
        !            13:        $tmp    = NULL;
        !            14:        $link   = NULL;
        !            15: 
        !            16:        if (!is_null($tmp = @mysqli_stmt_affected_rows()))
        !            17:                printf("[001] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
        !            18: 
        !            19:        if (!is_null($tmp = @mysqli_stmt_affected_rows($link)))
        !            20:                printf("[002] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
        !            21: 
        !            22:        if (!$link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket)) {
        !            23:                printf("Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
        !            24:                        $host, $user, $db, $port, $socket);
        !            25:        }
        !            26:        $stmt = mysqli_stmt_init($link);
        !            27: 
        !            28:        if (!mysqli_stmt_prepare($stmt, 'DROP TABLE IF EXISTS test') ||
        !            29:                !mysqli_stmt_execute($stmt)) {
        !            30:                printf("[003] Failed to drop old test table: [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
        !            31:        }
        !            32: 
        !            33:        if (!mysqli_stmt_prepare($stmt, 'CREATE TABLE test(id INT, label CHAR(1), PRIMARY KEY(id)) ENGINE = ' . $engine) ||
        !            34:                !mysqli_stmt_execute($stmt)) {
        !            35:                printf("[004] Failed to create test table: [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
        !            36:        }
        !            37: 
        !            38:        if (0 !== ($tmp = mysqli_stmt_affected_rows($stmt)))
        !            39:                printf("[005] Expecting int/0, got %s/'%s'\n", gettype($tmp), $tmp);
        !            40: 
        !            41:        mysqli_stmt_close($stmt);
        !            42:        $stmt = mysqli_stmt_init($link);
        !            43: 
        !            44:        if (!mysqli_stmt_prepare($stmt, "INSERT INTO test(id, label) VALUES (1, 'a')") ||
        !            45:                !mysqli_stmt_execute($stmt))
        !            46:                printf("[006] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
        !            47: 
        !            48:        mysqli_stmt_close($stmt);
        !            49:        $stmt = mysqli_stmt_init($link);
        !            50: 
        !            51:        if (!mysqli_stmt_prepare($stmt, "INSERT INTO test(id, label) VALUES (100, 'z')") ||
        !            52:                !mysqli_stmt_execute($stmt))
        !            53:                printf("[007] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
        !            54: 
        !            55:        if (1 !== ($tmp = mysqli_stmt_affected_rows($stmt)))
        !            56:                printf("[008] Expecting int/1, got %s/%s\n", gettype($tmp), $tmp);
        !            57: 
        !            58:        mysqli_stmt_close($stmt);
        !            59:        $stmt = mysqli_stmt_init($link);
        !            60: 
        !            61:        if (!mysqli_stmt_prepare($stmt, "INSERT INTO test(id, label) VALUES (100, 'z')") ||
        !            62:                !mysqli_stmt_execute($stmt))
        !            63:                // NOTE: the error message varies with the MySQL Server version, dump only the error code!
        !            64:                printf("[009] [%d] (error message varies with the MySQL Server version, check the error code)\n", mysqli_stmt_errno($stmt));
        !            65: 
        !            66:        /* an error occured: affected rows should return -1 */
        !            67:        if (-1 !== ($tmp = mysqli_stmt_affected_rows($stmt)))
        !            68:                printf("[010] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);
        !            69: 
        !            70:        mysqli_stmt_close($stmt);
        !            71:        $stmt = mysqli_stmt_init($link);
        !            72: 
        !            73:        if (!mysqli_stmt_prepare($stmt, "INSERT INTO test(id, label) VALUES (1, 'a') ON DUPLICATE KEY UPDATE id = 4") ||
        !            74:                !mysqli_stmt_execute($stmt))
        !            75:                printf("[011] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
        !            76: 
        !            77:        if (2 !== ($tmp = mysqli_stmt_affected_rows($stmt)))
        !            78:                printf("[012] Expecting int/2, got %s/%s\n", gettype($tmp), $tmp);
        !            79: 
        !            80:        mysqli_stmt_close($stmt);
        !            81:        $stmt = mysqli_stmt_init($link);
        !            82: 
        !            83:        if (!mysqli_stmt_prepare($stmt, "INSERT INTO test(id, label) VALUES (2, 'b'), (3, 'c')") ||
        !            84:                !mysqli_stmt_execute($stmt))
        !            85:                printf("[013] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
        !            86: 
        !            87:        if (2 !== ($tmp = mysqli_stmt_affected_rows($stmt)))
        !            88:                printf("[014] Expecting int/2, got %s/%s\n", gettype($tmp), $tmp);
        !            89: 
        !            90:        mysqli_stmt_close($stmt);
        !            91:        $stmt = mysqli_stmt_init($link);
        !            92: 
        !            93:        if (!mysqli_stmt_prepare($stmt, "INSERT IGNORE INTO test(id, label) VALUES (1, 'a')") ||
        !            94:                !mysqli_stmt_execute($stmt))
        !            95:                printf("[015] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
        !            96: 
        !            97:        if (1 !== ($tmp = mysqli_stmt_affected_rows($stmt)))
        !            98:                printf("[016] Expecting int/1, got %s/%s\n", gettype($tmp), $tmp);
        !            99: 
        !           100:        if (!($res = mysqli_query($link, "SELECT count(id) AS num FROM test")) ||
        !           101:                !($tmp = mysqli_fetch_assoc($res)))
        !           102:                printf("[017] [%d] %s\n", mysqli_error($link), mysqli_error($link));
        !           103:        $num = (int)$tmp['num'];
        !           104:        mysqli_free_result($res);
        !           105: 
        !           106:        mysqli_stmt_close($stmt);
        !           107:        $stmt = mysqli_stmt_init($link);
        !           108: 
        !           109:        if (!mysqli_stmt_prepare($stmt, "INSERT INTO test(id, label) SELECT id + 10, label FROM test") ||
        !           110:                !mysqli_stmt_execute($stmt))
        !           111:                printf("[018] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
        !           112: 
        !           113:        if ($num !== ($tmp = mysqli_stmt_affected_rows($stmt)))
        !           114:                printf("[019] Expecting int/%d, got %s/%s\n", $num, gettype($tmp), $tmp);
        !           115: 
        !           116:        mysqli_stmt_close($stmt);
        !           117:        $stmt = mysqli_stmt_init($link);
        !           118: 
        !           119:        if (!mysqli_stmt_prepare($stmt, "REPLACE INTO test(id, label) values (4, 'd')") ||
        !           120:                !mysqli_stmt_execute($stmt))
        !           121:                printf("[020] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
        !           122: 
        !           123:        if (2 !== ($tmp = mysqli_stmt_affected_rows($stmt)))
        !           124:                printf("[021] Expecting int/2, got %s/%s\n", gettype($tmp), $tmp);
        !           125: 
        !           126:        mysqli_stmt_close($stmt);
        !           127:        $stmt = mysqli_stmt_init($link);
        !           128: 
        !           129:        if (!mysqli_stmt_prepare($stmt, "REPLACE INTO test(id, label) values (5, 'e')") ||
        !           130:                !mysqli_stmt_execute($stmt))
        !           131:                printf("[022] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
        !           132: 
        !           133:        if (1 !== ($tmp = mysqli_stmt_affected_rows($stmt)))
        !           134:                printf("[023] Expecting int/1, got %s/%s\n", gettype($tmp), $tmp);
        !           135: 
        !           136:        mysqli_stmt_close($stmt);
        !           137:        $stmt = mysqli_stmt_init($link);
        !           138: 
        !           139:        if (!mysqli_stmt_prepare($stmt, "UPDATE test SET label = 'a' WHERE id = 2") ||
        !           140:                !mysqli_stmt_execute($stmt))
        !           141:                printf("[024] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
        !           142: 
        !           143:        if (1 !== ($tmp = mysqli_stmt_affected_rows($stmt)))
        !           144:                printf("[025] Expecting int/1, got %s/%s\n", gettype($tmp), $tmp);
        !           145: 
        !           146:        if (!mysqli_stmt_prepare($stmt, "UPDATE test SET label = 'a' WHERE id = 2") ||
        !           147:                !mysqli_stmt_execute($stmt))
        !           148:                printf("[026] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
        !           149: 
        !           150:        if (0 !== ($tmp = mysqli_stmt_affected_rows($stmt)))
        !           151:                printf("[027] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);
        !           152: 
        !           153:        mysqli_stmt_close($stmt);
        !           154:        $stmt = mysqli_stmt_init($link);
        !           155: 
        !           156:        if (!mysqli_stmt_prepare($stmt, "UPDATE test SET label = 'a' WHERE id = 100") ||
        !           157:                !mysqli_stmt_execute($stmt))
        !           158:                printf("[028] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
        !           159: 
        !           160:        if (1 !== ($tmp = mysqli_stmt_affected_rows($stmt)))
        !           161:                printf("[029] Expecting int/1, got %s/%s\n", gettype($tmp), $tmp);
        !           162: 
        !           163:        if (!mysqli_stmt_prepare($stmt, 'SELECT label FROM test WHERE id = 100') ||
        !           164:                !mysqli_stmt_execute($stmt))
        !           165:                printf("[030] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
        !           166: 
        !           167:        /* use it like num_rows */
        !           168:        /* PS are unbuffered, num_rows cannot determine the row count before all rows have been fetched and/or buffered */
        !           169:        if (-1 !== ($tmp = mysqli_stmt_affected_rows($stmt)))
        !           170:                printf("[031] Expecting int/-1, got %s/%s\n", gettype($tmp), $tmp);
        !           171: 
        !           172:        if (0 !== ($tmp = mysqli_stmt_num_rows($stmt)))
        !           173:                printf("[032] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);
        !           174: 
        !           175:        if (!mysqli_stmt_store_result($stmt))
        !           176:                printf("[033] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
        !           177: 
        !           178:        if (1 !== ($tmp = mysqli_stmt_affected_rows($stmt)))
        !           179:                printf("[034] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);
        !           180: 
        !           181:        if (1 !== ($tmp = mysqli_stmt_num_rows($stmt)))
        !           182:                printf("[035] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);
        !           183: 
        !           184:        mysqli_stmt_free_result($stmt);
        !           185:        mysqli_stmt_close($stmt);
        !           186:        $stmt = mysqli_stmt_init($link);
        !           187: 
        !           188:        if (!mysqli_stmt_prepare($stmt, 'SELECT label FROM test WHERE 1 = 2') ||
        !           189:                !mysqli_stmt_execute($stmt))
        !           190:                printf("[036] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
        !           191: 
        !           192:        /* use it like num_rows */
        !           193:        if (-1 !== ($tmp = mysqli_stmt_affected_rows($stmt)))
        !           194:                printf("[037] Expecting int/-1, got %s/%s\n", gettype($tmp), $tmp);
        !           195: 
        !           196:        if (true !== ($tmp = mysqli_stmt_store_result($stmt)))
        !           197:                printf("[038] Expecting boolean/true, got %s\%s\n", gettype($tmp), $tmp);
        !           198: 
        !           199:        if (0 !== ($tmp = mysqli_stmt_num_rows($stmt)))
        !           200:                printf("[039] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);
        !           201: 
        !           202:        if (0 !== ($tmp = mysqli_stmt_affected_rows($stmt)))
        !           203:                printf("[040] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);
        !           204: 
        !           205:        /* try to use stmt_affected_rows like stmt_num_rows */
        !           206:        /* stmt_affected_rows is not really meant for SELECT! */
        !           207:        if (mysqli_stmt_prepare($stmt, 'SELECT unknown_column FROM this_table_does_not_exist') ||
        !           208:                mysqli_stmt_execute($stmt))
        !           209:                printf("[041] The invalid SELECT statement is issued on purpose\n");
        !           210: 
        !           211:        if (-1 !== ($tmp = mysqli_stmt_affected_rows($stmt)))
        !           212:                printf("[042] Expecting int/-1, got %s/%s\n", gettype($tmp), $tmp);
        !           213: 
        !           214:        if ($IS_MYSQLND) {
        !           215:                if (false !== ($tmp = mysqli_stmt_store_result($stmt)))
        !           216:                        printf("[043] Expecting boolean/false, got %s\%s\n", gettype($tmp), $tmp);
        !           217:        } else {
        !           218:                if (true !== ($tmp = mysqli_stmt_store_result($stmt)))
        !           219:                        printf("[043] Libmysql does not care if the previous statement was bogus, expecting boolean/true, got %s\%s\n", gettype($tmp), $tmp);
        !           220:        }
        !           221: 
        !           222:        if (0 !== ($tmp = mysqli_stmt_num_rows($stmt)))
        !           223:                printf("[044] Expecting int/0, got %s/%s\n", gettype($tmp), $tmp);
        !           224: 
        !           225:        if (-1 !== ($tmp = mysqli_stmt_affected_rows($stmt)))
        !           226:                printf("[045] Expecting int/-1, got %s/%s\n", gettype($tmp), $tmp);
        !           227: 
        !           228:        mysqli_stmt_close($stmt);
        !           229:        $stmt = mysqli_stmt_init($link);
        !           230: 
        !           231:        if (!mysqli_stmt_prepare($stmt, "DROP TABLE IF EXISTS test") ||
        !           232:                !mysqli_stmt_execute($stmt))
        !           233:                printf("[046] [%d] %s\n", mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
        !           234: 
        !           235:        mysqli_stmt_close($stmt);
        !           236: 
        !           237:        if (!is_null($tmp = mysqli_stmt_affected_rows($stmt)))
        !           238:                printf("[047] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
        !           239: 
        !           240:        mysqli_close($link);
        !           241: 
        !           242:        print "done!";
        !           243: ?>
        !           244: --CLEAN--
        !           245: <?php
        !           246:        require_once("clean_table.inc");
        !           247: ?>
        !           248: --EXPECTF--
        !           249: [009] [%d] (error message varies with the MySQL Server version, check the error code)
        !           250: 
        !           251: Warning: mysqli_stmt_affected_rows(): Couldn't fetch mysqli_stmt in %s on line %d
        !           252: done!

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>