1--TEST--
2MySQL PDOStatement->bindColumn()
3--EXTENSIONS--
4pdo_mysql
5--SKIPIF--
6<?php
7require_once __DIR__ . '/inc/mysql_pdo_test.inc';
8MySQLPDOTest::skip();
9?>
10--FILE--
11<?php
12    require_once __DIR__ . '/inc/mysql_pdo_test.inc';
13    $db = MySQLPDOTest::factory();
14
15    $table = 'pdo_mysql_stmt_bindcolumn';
16    MySQLPDOTest::createTestTable($table, $db);
17
18    try {
19        $db->setAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY, 1);
20        if (1 != $db->getAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY))
21            printf("[002] Unable to turn on emulated prepared statements\n");
22
23        $stmt = $db->prepare("SELECT id, label FROM {$table} ORDER BY id ASC LIMIT 2");
24        $stmt->execute();
25        $id = $label = null;
26
27        if (!$stmt->bindColumn(1, $id, PDO::PARAM_INT))
28            printf("[003] Cannot bind integer column, %s %s\n",
29                $stmt->errorCode(), var_export($stmt->errorInfo(), true));
30
31        if (!$stmt->bindColumn(2, $label, PDO::PARAM_STR))
32            printf("[004] Cannot bind string column, %s %s\n",
33                $stmt->errorCode(), var_export($stmt->errorInfo(), true));
34
35        $data = array();
36        while ($stmt->fetch(PDO::FETCH_BOUND)) {
37            printf("id = %s (%s) / label = %s (%s)\n",
38                var_export($id, true), gettype($id),
39                var_export($label, true), gettype($label));
40            $data[] = array('id' => $id, 'label' => $label);
41        }
42
43        $stmt = $db->query("SELECT id, label FROM {$table} ORDER BY id ASC LIMIT 2");
44        $index = 0;
45        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
46            if ($row['id'] != $data[$index]['id']) {
47                printf("[005] Fetch bound and fetch assoc differ - column 'id', bound: %s/%s, assoc: %s/%s\n",
48                    var_export($data[$index]['id'], true), gettype($data[$index]['id']),
49                    var_export($row['id'], true), gettype($row['id']));
50            }
51            if ($row['label'] != $data[$index]['label']) {
52                printf("[006] Fetch bound and fetch assoc differ - column 'label', bound: %s/%s, assoc: %s/%s\n",
53                    var_export($data[$index]['label'], true), gettype($data[$index]['label']),
54                    var_export($row['label'], true), gettype($row['label']));
55            }
56            $index++;
57        }
58
59        $db->setAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY, 0);
60        if (0 != $db->getAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY))
61            printf("[007] Unable to turn off emulated prepared statements\n");
62
63        $stmt = $db->prepare("SELECT id, label FROM {$table} ORDER BY id ASC LIMIT 2, 2");
64        $stmt->execute();
65        $id = $label = null;
66
67        if (!$stmt->bindColumn(1, $id, PDO::PARAM_INT))
68            printf("[008] Cannot bind integer column, %s %s\n",
69                $stmt->errorCode(), var_export($stmt->errorInfo(), true));
70
71        if (!$stmt->bindColumn(2, $label, PDO::PARAM_STR))
72            printf("[009] Cannot bind string column, %s %s\n",
73                $stmt->errorCode(), var_export($stmt->errorInfo(), true));
74
75        $data = array();
76        while ($stmt->fetch(PDO::FETCH_BOUND)) {
77            printf("id = %s (%s) / label = %s (%s)\n",
78                var_export($id, true), gettype($id),
79                var_export($label, true), gettype($label));
80            $data[] = array('id' => $id, 'label' => $label);
81        }
82
83        $stmt = $db->query("SELECT id, label FROM {$table} ORDER BY id ASC LIMIT 2, 2");
84        $index = 0;
85        while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
86            if ($row['id'] != $data[$index]['id']) {
87                printf("[010] Fetch bound and fetch assoc differ - column 'id', bound: %s/%s, assoc: %s/%s\n",
88                    var_export($data[$index]['id'], true), gettype($data[$index]['id']),
89                    var_export($row['id'], true), gettype($row['id']));
90            }
91            if ($row['label'] != $data[$index]['label']) {
92                printf("[011] Fetch bound and fetch assoc differ - column 'label', bound: %s/%s, assoc: %s/%s\n",
93                    var_export($data[$index]['label'], true), gettype($data[$index]['label']),
94                    var_export($row['label'], true), gettype($row['label']));
95            }
96            $index++;
97        }
98
99    } catch (PDOException $e) {
100        printf("[001] %s [%s] %s\n",
101            $e->getMessage(), $db->errorCode(), implode(' ', $db->errorInfo()));
102    }
103
104    print "done!";
105?>
106--CLEAN--
107<?php
108require_once __DIR__ . '/inc/mysql_pdo_test.inc';
109$db = MySQLPDOTest::factory();
110$db->exec('DROP TABLE IF EXISTS pdo_mysql_stmt_bindcolumn');
111?>
112--EXPECT--
113id = 1 (integer) / label = 'a' (string)
114id = 2 (integer) / label = 'b' (string)
115id = 3 (integer) / label = 'c' (string)
116id = 4 (integer) / label = 'd' (string)
117done!
118