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