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