Annotation of embedaddon/php/ext/mysql/tests/mysql_fetch_array.phpt, revision 1.1

1.1     ! misho       1: --TEST--
        !             2: mysql_fetch_array()
        !             3: --SKIPIF--
        !             4: <?php
        !             5: require_once('skipif.inc');
        !             6: require_once('skipifconnectfailure.inc');
        !             7: ?>
        !             8: --FILE--
        !             9: <?php
        !            10: include_once "connect.inc";
        !            11: 
        !            12: $tmp    = NULL;
        !            13: $link   = NULL;
        !            14: 
        !            15: if (NULL !== ($tmp = @mysql_fetch_array()))
        !            16:        printf("[001] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
        !            17: 
        !            18: if (NULL != ($tmp = @mysql_fetch_array($link)))
        !            19:        printf("[002] Expecting NULL, got %s/%s\n", gettype($tmp), $tmp);
        !            20: 
        !            21: require('table.inc');
        !            22: if (!$res = mysql_query("SELECT * FROM test ORDER BY id LIMIT 5", $link)) {
        !            23:        printf("[004] [%d] %s\n", mysql_errno($link), mysql_error($link));
        !            24: }
        !            25: 
        !            26: print "[005]\n";
        !            27: var_dump(mysql_fetch_array($res));
        !            28: 
        !            29: print "[006]\n";
        !            30: var_dump(mysql_fetch_array($res, MYSQL_NUM));
        !            31: 
        !            32: print "[007]\n";
        !            33: var_dump(mysql_fetch_array($res, MYSQL_BOTH));
        !            34: 
        !            35: print "[008]\n";
        !            36: var_dump(mysql_fetch_array($res, MYSQL_ASSOC));
        !            37: 
        !            38: print "[009]\n";
        !            39: var_dump(mysql_fetch_array($res));
        !            40: 
        !            41: mysql_free_result($res);
        !            42: 
        !            43: if (!$res = mysql_query("SELECT 1 AS a, 2 AS a, 3 AS c, 4 AS C, NULL AS d, true AS e", $link)) {
        !            44:        printf("[010] Cannot run query, [%d] %s\n", mysql_errno($link), mysql_error($link));
        !            45: }
        !            46: print "[011]\n";
        !            47: var_dump(mysql_fetch_array($res, MYSQL_BOTH));
        !            48: 
        !            49: mysql_free_result($res);
        !            50: if (!$res = mysql_query("SELECT 1 AS a, 2 AS b, 3 AS c, 4 AS C", $link)) {
        !            51:        printf("[012] Cannot run query, [%d] %s\n",
        !            52:                mysql_errno($link), $mysql_error($link));
        !            53:        exit(1);
        !            54: }
        !            55: 
        !            56: do {
        !            57:        $illegal_mode = mt_rand(0, 10000);
        !            58: } while (in_array($illegal_mode, array(MYSQL_ASSOC, MYSQL_NUM, MYSQL_BOTH)));
        !            59: $tmp = mysql_fetch_array($res, $illegal_mode);
        !            60: if (!is_array($tmp))
        !            61:        printf("[013] Expecting array, got %s/%s. [%d] %s\n",
        !            62:                gettype($tmp), $tmp, mysql_errno($link), mysql_error($link));
        !            63: 
        !            64: $tmp = @mysql_fetch_array($res, $illegal_mode);
        !            65: if (false !== $tmp)
        !            66:        printf("[014] Expecting boolean/false, got %s/%s. [%d] %s\n",
        !            67:                gettype($tmp), $tmp, mysql_errno($link), mysql_error($link));
        !            68: 
        !            69: mysql_free_result($res);
        !            70: 
        !            71: function func_mysql_fetch_array($link, $engine, $sql_type, $sql_value, $php_value, $offset, $regexp_comparison = NULL, $binary_type = false) {
        !            72: 
        !            73:        if (!mysql_query("DROP TABLE IF EXISTS test", $link)) {
        !            74:                printf("[%04d] [%d] %s\n", $offset, mysql_errno($link), mysql_error($link));
        !            75:                return false;
        !            76:        }
        !            77: 
        !            78:        if (!mysql_query($sql = sprintf("CREATE TABLE test(id INT NOT NULL, label %s, PRIMARY KEY(id)) ENGINE = %s", $sql_type, $engine), $link)) {
        !            79:                // don't bail, engine might not support the datatype
        !            80:                return false;
        !            81:        }
        !            82: 
        !            83:        if (is_null($php_value) && !mysql_query($sql = sprintf("INSERT INTO test(id, label) VALUES (1, NULL)"), $link)) {
        !            84:                printf("[%04d] [%d] %s\n", $offset + 1, mysql_errno($link), mysql_error($link));
        !            85:                return false;
        !            86:        }
        !            87: 
        !            88:        if (!is_null($php_value)) {
        !            89:                if (is_int($sql_value) && !mysql_query(sprintf("INSERT INTO test(id, label) VALUES (1, '%d')", $sql_value), $link)) {
        !            90:                        printf("[%04d] [%d] %s\n", $offset + 1, mysql_errno($link), mysql_error($link));
        !            91:                        return false;
        !            92:                } else if (!is_int($sql_value) && !mysql_query(sprintf("INSERT INTO test(id, label) VALUES (1, '%s')", $sql_value), $link)) {
        !            93:                        printf("[%04d] [%d] %s\n", $offset + 1, mysql_errno($link), mysql_error($link));
        !            94:                        return false;
        !            95:                }
        !            96:        }
        !            97: 
        !            98:        if (!$res = mysql_query("SELECT id, label FROM test", $link)) {
        !            99:                printf("[%04d] [%d] %s\n", $offset + 2, mysql_errno($link), mysql_error($link));
        !           100:                return false;
        !           101:        }
        !           102: 
        !           103:        if (!$row = mysql_fetch_array($res, MYSQL_BOTH)) {
        !           104:                printf("[%04d] [%d] %s\n", $offset + 3, mysql_errno($link), mysql_error($link));
        !           105:                return false;
        !           106:        }
        !           107: 
        !           108:        if ($regexp_comparison) {
        !           109:                if (!preg_match($regexp_comparison, (string)$row['label']) || !preg_match($regexp_comparison, (string)$row[1])) {
        !           110:                printf("[%04d] Expecting %s/%s [reg exp = %s], got %s/%s resp. %s/%s. [%d] %s\n", $offset + 4,
        !           111:                        gettype($php_value), $php_value, $regexp_comparison,
        !           112:                        gettype($row[1]), $row[1],
        !           113:                        gettype($row['label']), $row['label'], mysql_errno($link), mysql_error($link));
        !           114:                return false;
        !           115:                }
        !           116:        } else if ((gettype($php_value) == 'unicode') && $binary_type) {
        !           117:                // Unicode is on and we are told that the MySQL column type is a binary type.
        !           118:                // Don't expect a unicode value from the database, you'll get binary string
        !           119:                if (($row['label'] != $php_value) || ($row[1] != $php_value)) {
        !           120:                        printf("[%04d] Expecting %s/%s, got %s/%s resp. %s/%s. [%d] %s\n", $offset + 5,
        !           121:                                gettype($php_value), $php_value,
        !           122:                                gettype($row[1]), $row[1],
        !           123:                                gettype($row['label']), $row['label'], mysql_errno($link), mysql_error($link));
        !           124:                        return false;
        !           125:                }
        !           126:                if (gettype($row['label']) == 'unicode') {
        !           127:                        printf("[%04d] SQL Type: '%s', binary columns are supposed to return binary string and not unicode\n",
        !           128:                                $offset + 6, $sql_type);
        !           129:                        return false;
        !           130:                }
        !           131:        } else {
        !           132:                if (($row['label'] !== $php_value) || ($row[1] != $php_value)) {
        !           133:                        printf("[%04d] Expecting %s/%s, got %s/%s resp. %s/%s. [%d] %s\n", $offset + 7,
        !           134:                                gettype($php_value), $php_value,
        !           135:                                gettype($row[1]), $row[1],
        !           136:                                gettype($row['label']), $row['label'], mysql_errno($link), mysql_error($link));
        !           137:                        return false;
        !           138:                }
        !           139:        }
        !           140: 
        !           141:        return true;
        !           142: }
        !           143: 
        !           144: function func_mysql_fetch_array_make_string($len) {
        !           145: 
        !           146:        $ret = '';
        !           147:        for ($i = 0; $i < $len; $i++)
        !           148:                $ret .= chr(mt_rand(65, 90));
        !           149: 
        !           150:        return $ret;
        !           151: }
        !           152: 
        !           153: func_mysql_fetch_array($link, $engine, "TINYINT", -11, "-11", 20);
        !           154: func_mysql_fetch_array($link, $engine, "TINYINT", NULL, NULL, 30);
        !           155: func_mysql_fetch_array($link, $engine, "TINYINT UNSIGNED", 1, "1", 40);
        !           156: func_mysql_fetch_array($link, $engine, "TINYINT UNSIGNED", NULL, NULL, 50);
        !           157: 
        !           158: func_mysql_fetch_array($link, $engine, "BOOL", 1, "1", 60);
        !           159: func_mysql_fetch_array($link, $engine, "BOOL", NULL, NULL, 70);
        !           160: func_mysql_fetch_array($link, $engine, "BOOLEAN", 0, "0", 80);
        !           161: func_mysql_fetch_array($link, $engine, "BOOLEAN", NULL, NULL, 90);
        !           162: 
        !           163: func_mysql_fetch_array($link, $engine, "SMALLINT", -32768, "-32768", 100);
        !           164: func_mysql_fetch_array($link, $engine, "SMALLINT", 32767, "32767", 110);
        !           165: func_mysql_fetch_array($link, $engine, "SMALLINT", NULL, NULL, 120);
        !           166: func_mysql_fetch_array($link, $engine, "SMALLINT UNSIGNED", 65535, "65535", 130);
        !           167: func_mysql_fetch_array($link, $engine, "SMALLINT UNSIGNED", NULL, NULL, 140);
        !           168: 
        !           169: func_mysql_fetch_array($link, $engine, "MEDIUMINT", -8388608, "-8388608", 150);
        !           170: func_mysql_fetch_array($link, $engine, "MEDIUMINT", 8388607, "8388607", 160);
        !           171: func_mysql_fetch_array($link, $engine, "MEDIUMINT", NULL, NULL, 170);
        !           172: func_mysql_fetch_array($link, $engine, "MEDIUMINT UNSIGNED", 16777215, "16777215", 180);
        !           173: func_mysql_fetch_array($link, $engine, "MEDIUMINT UNSIGNED", NULL, NULL, 190);
        !           174: 
        !           175: func_mysql_fetch_array($link, $engine, "INTEGER", -2147483648, "-2147483648", 200);
        !           176: func_mysql_fetch_array($link, $engine, "INTEGER", 2147483647, "2147483647", 210);
        !           177: func_mysql_fetch_array($link, $engine, "INTEGER", NULL, NULL, 220);
        !           178: func_mysql_fetch_array($link, $engine, "INTEGER UNSIGNED", 4294967295, "4294967295", 230);
        !           179: func_mysql_fetch_array($link, $engine, "INTEGER UNSIGNED", NULL, NULL, 240);
        !           180: 
        !           181: // func_mysql_fetch_array($link, $engine, "BIGINT", -9223372036854775808, "-9.22337e+018", 250, "/-9\.22337e\+[0]?18/iu");
        !           182: func_mysql_fetch_array($link, $engine, "BIGINT", NULL, NULL, 260);
        !           183: // func_mysql_fetch_array($link, $engine, "BIGINT UNSIGNED", 18446744073709551615, "1.84467e+019", 270, "/1\.84467e\+[0]?19/iu");
        !           184: func_mysql_fetch_array($link, $engine, "BIGINT UNSIGNED", NULL, NULL, 280);
        !           185: 
        !           186: func_mysql_fetch_array($link, $engine, "FLOAT", -9223372036854775808 - 1.1, "-9.22337e+18", 290, "/-9\.22337e\+?[0]?18/iu");
        !           187: func_mysql_fetch_array($link, $engine, "FLOAT", NULL, NULL, 300);
        !           188: func_mysql_fetch_array($link, $engine, "FLOAT UNSIGNED", 18446744073709551615 + 1.1, "1.84467e+19", 310, "/1\.84467e\+?[0]?19/iu");
        !           189: func_mysql_fetch_array($link, $engine, "FLOAT UNSIGNED ", NULL, NULL, 320);
        !           190: 
        !           191: func_mysql_fetch_array($link, $engine, "DOUBLE(10,2)", -99999999.99, "-99999999.99", 330);
        !           192: func_mysql_fetch_array($link, $engine, "DOUBLE(10,2)", NULL, NULL, 340);
        !           193: func_mysql_fetch_array($link, $engine, "DOUBLE(10,2) UNSIGNED", 99999999.99, "99999999.99", 350);
        !           194: func_mysql_fetch_array($link, $engine, "DOUBLE(10,2) UNSIGNED", NULL, NULL, 360);
        !           195: 
        !           196: func_mysql_fetch_array($link, $engine, "DECIMAL(10,2)", -99999999.99, "-99999999.99", 370);
        !           197: func_mysql_fetch_array($link, $engine, "DECIMAL(10,2)", NULL, NULL, 380);
        !           198: func_mysql_fetch_array($link, $engine, "DECIMAL(10,2)", 99999999.99, "99999999.99", 390);
        !           199: func_mysql_fetch_array($link, $engine, "DECIMAL(10,2)", NULL, NULL, 400);
        !           200: 
        !           201: // don't care about date() strict TZ warnings...
        !           202: func_mysql_fetch_array($link, $engine, "DATE", @date('Y-m-d'), @date('Y-m-d'), 410);
        !           203: func_mysql_fetch_array($link, $engine, "DATE NOT NULL", @date('Y-m-d'), @date('Y-m-d'), 420);
        !           204: func_mysql_fetch_array($link, $engine, "DATE", NULL, NULL, 430);
        !           205: 
        !           206: func_mysql_fetch_array($link, $engine, "DATETIME", @date('Y-m-d H:i:s'), @date('Y-m-d H:i:s'), 440);
        !           207: func_mysql_fetch_array($link, $engine, "DATETIME NOT NULL", @date('Y-m-d H:i:s'), @date('Y-m-d H:i:s'), 450);
        !           208: func_mysql_fetch_array($link, $engine, "DATETIME", NULL, NULL, 460);
        !           209: 
        !           210: func_mysql_fetch_array($link, $engine, "TIMESTAMP", @date('Y-m-d H:i:s'), @date('Y-m-d H:i:s'), 470);
        !           211: 
        !           212: func_mysql_fetch_array($link, $engine, "TIME", @date('H:i:s'), @date('H:i:s'), 480);
        !           213: func_mysql_fetch_array($link, $engine, "TIME NOT NULL", @date('H:i:s'), @date('H:i:s'), 490);
        !           214: func_mysql_fetch_array($link, $engine, "TIME", NULL, NULL, 500);
        !           215: 
        !           216: func_mysql_fetch_array($link, $engine, "YEAR", @date('Y'), @date('Y'), 510);
        !           217: func_mysql_fetch_array($link, $engine, "YEAR NOT NULL", @date('Y'), @date('Y'), 520);
        !           218: func_mysql_fetch_array($link, $engine, "YEAR", NULL, NULL, 530);
        !           219: 
        !           220: $string255 = func_mysql_fetch_array_make_string(255);
        !           221: 
        !           222: func_mysql_fetch_array($link, $engine, "CHAR(1)", "a", "a", 540);
        !           223: func_mysql_fetch_array($link, $engine, "CHAR(255)", $string255,  $string255, 550);
        !           224: func_mysql_fetch_array($link, $engine, "CHAR(1) NOT NULL", "a", "a", 560);
        !           225: func_mysql_fetch_array($link, $engine, "CHAR(1)", NULL, NULL, 570);
        !           226: 
        !           227: $string65k = func_mysql_fetch_array_make_string(65400);
        !           228: 
        !           229: func_mysql_fetch_array($link, $engine, "VARCHAR(1)", "a", "a", 580);
        !           230: func_mysql_fetch_array($link, $engine, "VARCHAR(255)", $string255, $string255, 590);
        !           231: func_mysql_fetch_array($link, $engine, "VARCHAR(65400)", $string65k, $string65k, 600);
        !           232: func_mysql_fetch_array($link, $engine, "VARCHAR(1) NOT NULL", "a", "a", 610);
        !           233: func_mysql_fetch_array($link, $engine, "VARCHAR(1)", NULL, NULL, 620);
        !           234: 
        !           235: func_mysql_fetch_array($link, $engine, "BINARY(1)", "a", "a", 630, null , true);
        !           236: func_mysql_fetch_array($link, $engine, "BINARY(1) NOT NULL", "b", "b", 650, null , true);
        !           237: func_mysql_fetch_array($link, $engine, "BINARY(1)", NULL, NULL, 660, null , true);
        !           238: 
        !           239: func_mysql_fetch_array($link, $engine, "VARBINARY(1)", "a", "a", 670, null , true);
        !           240: func_mysql_fetch_array($link, $engine, "VARBINARY(1) NOT NULL", "b", "b", 690, null , true);
        !           241: func_mysql_fetch_array($link, $engine, "VARBINARY(1)", NULL, NULL, 700, null , true);
        !           242: 
        !           243: func_mysql_fetch_array($link, $engine, "TINYBLOB", "a", "a", 710, null , true);
        !           244: func_mysql_fetch_array($link, $engine, "TINYBLOB NOT NULL", "b", "b", 730, null , true);
        !           245: func_mysql_fetch_array($link, $engine, "TINYBLOB", NULL, NULL, 740, null , true);
        !           246: 
        !           247: func_mysql_fetch_array($link, $engine, "TINYTEXT", "a", "a", 750);
        !           248: func_mysql_fetch_array($link, $engine, "TINYTEXT NOT NULL", "a", "a", 760);
        !           249: func_mysql_fetch_array($link, $engine, "TINYTEXT", NULL, NULL, 770);
        !           250: 
        !           251: func_mysql_fetch_array($link, $engine, "BLOB", "a", "a", 780, null , true);
        !           252: func_mysql_fetch_array($link, $engine, "BLOB", NULL, NULL, 790, null , true);
        !           253: 
        !           254: func_mysql_fetch_array($link, $engine, "TEXT", "a", "a", 800);
        !           255: func_mysql_fetch_array($link, $engine, "TEXT", NULL, NULL, 820);
        !           256: 
        !           257: func_mysql_fetch_array($link, $engine, "MEDIUMBLOB", "a", "a", 830, null , true);
        !           258: func_mysql_fetch_array($link, $engine, "MEDIUMBLOB", NULL, NULL, 850, null , true);
        !           259: 
        !           260: func_mysql_fetch_array($link, $engine, "MEDIUMTEXT", "a", "a", 860);
        !           261: func_mysql_fetch_array($link, $engine, "MEDIUMTEXT", NULL, NULL, 880);
        !           262: 
        !           263: func_mysql_fetch_array($link, $engine, "LONGBLOB", "a", "a", 890, null , true);
        !           264: func_mysql_fetch_array($link, $engine, "LONGBLOB", NULL, NULL, 910, null , true);
        !           265: 
        !           266: func_mysql_fetch_array($link, $engine, "ENUM('a', 'b')", "a", "a", 920);
        !           267: func_mysql_fetch_array($link, $engine, "ENUM('a', 'b')", NULL, NULL, 930);
        !           268: 
        !           269: func_mysql_fetch_array($link, $engine, "SET('a', 'b')", "a", "a", 940);
        !           270: func_mysql_fetch_array($link, $engine, "SET('a', 'b')", NULL, NULL, 950);
        !           271: 
        !           272: mysql_close($link);
        !           273: 
        !           274: if (false !== ($tmp = mysql_fetch_array($res, MYSQL_ASSOC)))
        !           275: printf("[015] Expecting boolean/false, got %s/%s\n", gettype($tmp), $tmp);
        !           276: 
        !           277: print "done!";
        !           278: ?>
        !           279: --CLEAN--
        !           280: <?php
        !           281: require_once("clean_table.inc");
        !           282: ?>
        !           283: --EXPECTF--
        !           284: [005]
        !           285: array(4) {
        !           286:   [0]=>
        !           287:   %unicode|string%(1) "1"
        !           288:   [%u|b%"id"]=>
        !           289:   %unicode|string%(1) "1"
        !           290:   [1]=>
        !           291:   %unicode|string%(1) "a"
        !           292:   [%u|b%"label"]=>
        !           293:   %unicode|string%(1) "a"
        !           294: }
        !           295: [006]
        !           296: array(2) {
        !           297:   [0]=>
        !           298:   %unicode|string%(1) "2"
        !           299:   [1]=>
        !           300:   %unicode|string%(1) "b"
        !           301: }
        !           302: [007]
        !           303: array(4) {
        !           304:   [0]=>
        !           305:   %unicode|string%(1) "3"
        !           306:   [%u|b%"id"]=>
        !           307:   %unicode|string%(1) "3"
        !           308:   [1]=>
        !           309:   %unicode|string%(1) "c"
        !           310:   [%u|b%"label"]=>
        !           311:   %unicode|string%(1) "c"
        !           312: }
        !           313: [008]
        !           314: array(2) {
        !           315:   [%u|b%"id"]=>
        !           316:   %unicode|string%(1) "4"
        !           317:   [%u|b%"label"]=>
        !           318:   %unicode|string%(1) "d"
        !           319: }
        !           320: [009]
        !           321: array(4) {
        !           322:   [0]=>
        !           323:   %unicode|string%(1) "5"
        !           324:   [%u|b%"id"]=>
        !           325:   %unicode|string%(1) "5"
        !           326:   [1]=>
        !           327:   %unicode|string%(1) "e"
        !           328:   [%u|b%"label"]=>
        !           329:   %unicode|string%(1) "e"
        !           330: }
        !           331: [011]
        !           332: array(11) {
        !           333:   [0]=>
        !           334:   %unicode|string%(1) "1"
        !           335:   [%u|b%"a"]=>
        !           336:   %unicode|string%(1) "2"
        !           337:   [1]=>
        !           338:   %unicode|string%(1) "2"
        !           339:   [2]=>
        !           340:   %unicode|string%(1) "3"
        !           341:   [%u|b%"c"]=>
        !           342:   %unicode|string%(1) "3"
        !           343:   [3]=>
        !           344:   %unicode|string%(1) "4"
        !           345:   [%u|b%"C"]=>
        !           346:   %unicode|string%(1) "4"
        !           347:   [4]=>
        !           348:   NULL
        !           349:   [%u|b%"d"]=>
        !           350:   NULL
        !           351:   [5]=>
        !           352:   %unicode|string%(1) "1"
        !           353:   [%u|b%"e"]=>
        !           354:   %unicode|string%(1) "1"
        !           355: }
        !           356: 
        !           357: Warning: mysql_fetch_array(): The result type should be either MYSQL_NUM, MYSQL_ASSOC or MYSQL_BOTH in %s on line %d
        !           358: 
        !           359: Warning: mysql_fetch_array(): %d is not a valid MySQL result resource in %s on line %d
        !           360: done!

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