Annotation of embedaddon/php/ext/mysqli/tests/mysqli_stmt_bind_param_type_juggling.phpt, revision 1.1.1.1

1.1       misho       1: --TEST--
                      2: mysqli_stmt_bind_param() - binding variable twice
                      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('table.inc');
                     12: 
                     13:        function bind_twice($link, $engine, $sql_type1, $sql_type2, $bind_type1, $bind_type2, $bind_value1, $bind_value2, $offset) {
                     14: 
                     15:                if (!mysqli_query($link, "DROP TABLE IF EXISTS test")) {
                     16:                        printf("[%03d + 1] [%d] %s\n", $offset, mysqli_errno($link), mysqli_error($link));
                     17:                        return false;
                     18:                }
                     19:                mysqli_autocommit($link, true);
                     20: 
                     21:                $sql = sprintf("CREATE TABLE test(col1 %s, col2 %s) ENGINE=%s", $sql_type1, $sql_type2, $engine);
                     22:                if (!mysqli_query($link, $sql)) {
                     23:                        printf("[%03d + 2] [%d] %s\n", $offset, mysqli_errno($link), mysqli_error($link));
                     24:                        return false;
                     25:                }
                     26: 
                     27:                if (!$stmt = mysqli_stmt_init($link)) {
                     28:                        printf("[%03d + 3] [%d] %s\n", $offset, mysqli_errno($link), mysqli_error($link));
                     29:                        return false;
                     30:                }
                     31: 
                     32:                if (!mysqli_stmt_prepare($stmt, "INSERT INTO test(col1, col2) VALUES (?, ?)")) {
                     33:                        printf("[%03d + 4] [%d] %s\n", $offset, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
                     34:                        return false;
                     35:                }
                     36: 
                     37:                if (!mysqli_stmt_bind_param($stmt, $bind_type1 . $bind_type2, $bind_value1, $bind_value1)) {
                     38:                        printf("[%03d + 5] [%d] %s\n", $offset, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
                     39:                        return false;
                     40:                }
                     41: 
                     42:                if (!mysqli_stmt_execute($stmt)) {
                     43:                        printf("[%03d + 6] [%d] %s\n", $offset, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
                     44:                        return false;
                     45:                }
                     46: 
                     47:                if (!mysqli_stmt_bind_param($stmt, $bind_type1 . $bind_type2, $bind_value1, $bind_value2)) {
                     48:                        printf("[%03d + 7] [%d] %s\n", $offset, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
                     49:                        return false;
                     50:                }
                     51:                if (!mysqli_stmt_execute($stmt)) {
                     52:                        printf("[%03d + 8] [%d] %s\n", $offset, mysqli_stmt_errno($stmt), mysqli_stmt_error($stmt));
                     53:                        return false;
                     54:                }
                     55: 
                     56:                mysqli_stmt_close($stmt);
                     57:                if (!$res = mysqli_query($link, "SELECT col1, col2 FROM test")) {
                     58:                        printf("[%03d + 9] [%d] %s\n", $offset, mysqli_errno($link), mysqli_error($link));
                     59:                        return false;
                     60:                }
                     61: 
                     62:                if (2 !== ($tmp = mysqli_num_rows($res))) {
                     63:                        printf("[%03d + 10] Expecting 2 rows, got %d rows [%d] %s\n", $offset, $tmp, mysqli_errno($link), mysqli_error($link));
                     64:                }
                     65: 
                     66:                $row = mysqli_fetch_assoc($res);
                     67:                if (($row['col1'] != $bind_value1) || ($row['col2'] != $bind_value1)) {
                     68:                        printf("[%03d + 11] Expecting col1 = %s, col2 = %s got col1 = %s, col2 = %s - [%d] %s\n",
                     69:                                $offset, $bind_value1, $bind_value1,
                     70:                                $row['col1'], $row['col2'],
                     71:                                mysqli_errno($link), mysqli_error($link));
                     72:                        return false;
                     73:                }
                     74: 
                     75:                $row = mysqli_fetch_assoc($res);
                     76:                if (($row['col1'] != $bind_value1) || ($row['col2'] != $bind_value2)) {
                     77:                        printf("[%03d + 12] Expecting col1 = %s, col2 = %s got col1 = %s, col2 = %s - [%d] %s\n",
                     78:                                $offset, $bind_value1, $bind_value2,
                     79:                                $row['col1'], $row['col2'],
                     80:                                mysqli_errno($link), mysqli_error($link));
                     81:                        return false;
                     82:                }
                     83:                mysqli_free_result($res);
                     84:                return true;
                     85:        }
                     86: 
                     87:        bind_twice($link, $engine, 'CHAR(1)', 'CHAR(1)', 's', 's', 'a', 'b', 10);
                     88:        bind_twice($link, $engine, 'INT', 'INT', 'i', 'i', 1, 2, 20);
                     89:        bind_twice($link, $engine, 'FLOAT', 'FLOAT', 'd', 'd', 1.01, 1.02, 30);
                     90: 
                     91:        /* type juggling - note that int->char works */
                     92:        bind_twice($link, $engine, 'CHAR(1)', 'CHAR(1)', 's', 's', 1, 2, 40);
                     93:        /* type juggling - note that string->integer works */
                     94:        bind_twice($link, $engine, 'INT', 'INT', 'i', 'i', '1', '2', 50);
                     95:        /* type juggling - note that string->float works*/
                     96:        bind_twice($link, $engine, 'FLOAT', 'FLOAT', 'd', 'd', '1.01', '1.02', 60);
                     97: 
                     98:        /* now, let's have two columns of different type and do type juggling */
                     99:        /*
                    100:        what the test will do is:
                    101:                1) col1 INT, col2 CHAR(1)
                    102:     2) bind_param('is', 1, 1)
                    103:     3) execute()
                    104:                4) bind_param('is', 1, 'a')
                    105:                5) execute()
                    106: 
                    107:                col1 INT, col2 INT
                    108:     bind_param('ii', '1', '2') --> OK  (int column, string value)
                    109:                bind_param('ii', 1, 2)                  --> OK  (int column, int value)
                    110:     col1 CHAR(1), col2 CHAR(2)
                    111:     bind_param('ss', 1, 2)             --> OK (string column, int value)
                    112: 
                    113:                So, what about:
                    114:                col1 INT, COL2 CHAR(1)
                    115:                bind_param('is', 1, 1)     ---> ??
                    116:        */
                    117:        bind_twice($link, $engine, 'INT', 'CHAR(1)', 'i', 's', 1, 'a', 70);
                    118: 
                    119:        mysqli_close($link);
                    120:        print "done!";
                    121: ?>
                    122: --CLEAN--
                    123: <?php
                    124:        require_once("clean_table.inc");
                    125: ?>
                    126: --EXPECTF--
                    127: done!

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