1--TEST--
2MySQL Bug #49406 (Binding params doesn't work when selecting a date inside a CASE-WHEN, http://bugs.mysql.com/bug.php?id=49406)
3--SKIPIF--
4<?php
5require_once('skipif.inc');
6require_once('skipifconnectfailure.inc');
7?>
8--FILE--
9<?php
10    require_once("connect.inc");
11
12    if (!($link = my_mysqli_connect($host, $user, $passwd, $db, $port, $socket)))
13        printf("[001] Cannot connect to the server using host=%s, user=%s, passwd=***, dbname=%s, port=%s, socket=%s\n",
14            $host, $user, $db, $port, $socket);
15
16    $query = "SELECT CASE  WHEN 0 THEN CAST('2009-12-03' AS DATE)  ELSE CAST('2009-12-03' AS DATE) END";
17
18    if (!$res = $link->query($query))
19        printf("[002] [%d] %s\n", $link->errno, $link->error);
20
21    if (!$row = $res->fetch_row())
22        printf("[003] No result, [%d] %s\n", $link->errno, $link->error);
23
24    $res->free();
25
26    if ($row[0] != '2009-12-03') {
27        printf("[004] Expecting '2009-12-03' got '%s'\n", $row[0]);
28    }
29
30    if (!$stmt = $link->prepare($query))
31        printf("[005] [%d] %s\n", $link->errno, $link->error);
32
33    if (!$stmt->execute() || !$stmt->store_result())
34        printf("[006] [%d] %s\n", $stmt->errno, $stmt->error);
35
36
37    $datatypes = array(
38        MYSQLI_TYPE_TINY => "TINY",
39        MYSQLI_TYPE_SHORT => "SHORT",
40        MYSQLI_TYPE_LONG => "LONG",
41        MYSQLI_TYPE_FLOAT => "FLOAT",
42        MYSQLI_TYPE_DOUBLE => "DOUBLE",
43        MYSQLI_TYPE_TIMESTAMP => "TIMESTAMP",
44        MYSQLI_TYPE_LONGLONG => "LONGLONG",
45        MYSQLI_TYPE_INT24 => "INT24",
46        MYSQLI_TYPE_DATE => "DATE",
47        MYSQLI_TYPE_TIME => "TIME",
48        MYSQLI_TYPE_DATETIME => "DATETIME",
49        MYSQLI_TYPE_YEAR => "YEAR",
50        MYSQLI_TYPE_ENUM => "ENUM",
51        MYSQLI_TYPE_SET	=> "SET",
52        MYSQLI_TYPE_TINY_BLOB => "TINYBLOB",
53        MYSQLI_TYPE_MEDIUM_BLOB => "MEDIUMBLOB",
54        MYSQLI_TYPE_LONG_BLOB => "LONGBLOB",
55        MYSQLI_TYPE_BLOB => "BLOB",
56        MYSQLI_TYPE_VAR_STRING => "VAR_STRING",
57        MYSQLI_TYPE_STRING => "STRING",
58        MYSQLI_TYPE_NULL => "NULL",
59        MYSQLI_TYPE_NEWDATE => "NEWDATE",
60        MYSQLI_TYPE_INTERVAL => "INTERVAL",
61        MYSQLI_TYPE_GEOMETRY => "GEOMETRY",
62    );
63
64    $meta_res = $stmt->result_metadata();
65    for ($field_idx = 0; $field_idx < $meta_res->field_count; $field_idx++) {
66        $field = $meta_res->fetch_field();
67        printf("Field        : %d\n", $field_idx);
68        printf("Name         : %s\n", $field->name);
69        printf("Orgname      : %s\n", $field->orgname);
70        printf("Table        : %s\n", $field->table);
71        printf("Orgtable     : %s\n", $field->orgtable);
72        printf("Maxlength    : %d\n", $field->max_length);
73        printf("Length       : %d\n", $field->length);
74        printf("Charsetnr    : %d\n", $field->charsetnr);
75        printf("Flags        : %d\n", $field->flags);
76        printf("Type         : %d (%s)\n", $field->type, (isset($datatypes[$field->type])) ? $datatypes[$field->type] : 'unknown');
77        printf("Decimals     : %d\n", $field->decimals);
78    }
79
80    $row_stmt = null;
81    if (!$stmt->bind_result($row_stmt) || !$stmt->fetch())
82        printf("[007] [%d] %s\n", $stmt->errno, $stmt->error);
83
84    if ($row[0] != $row_stmt) {
85        printf("[008] PS and non-PS results differ, dumping data\n");
86        var_dump($row[0]);
87        var_dump($row_stmt);
88    }
89
90    $stmt->close();
91    $link->close();
92
93    echo "done";
94?>
95--EXPECTF--
96Field        : %s
97Name         : %s
98Orgname      :%s
99Table        :%s
100Orgtable     :%s
101Maxlength    : %d
102Length       : %d
103Charsetnr    : %d
104Flags        : %d
105Type         : %d (%s)
106Decimals     : %d
107done
108