1--TEST--
2MySQL PDOStatement->bindParam()
3--SKIPIF--
4<?php
5require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'skipif.inc');
6require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
7MySQLPDOTest::skip();
8?>
9--FILE--
10<?php
11	require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
12	$db = MySQLPDOTest::factory();
13	$db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
14
15	MySQLPDOTest::createTestTable($db);
16
17	function pdo_mysql_stmt_bindparam($db, $offset) {
18
19		$stmt = $db->prepare('SELECT id, label FROM test WHERE id > ? ORDER BY id ASC LIMIT 2');
20		$in = 0;
21		if (!$stmt->bindParam(1, $in))
22			printf("[%03d + 1] Cannot bind parameter, %s %s\n", $offset,
23				$stmt->errorCode(), var_export($stmt->errorInfo(), true));
24
25		$stmt->execute();
26		$id = $label = null;
27
28		if (!$stmt->bindColumn(1, $id, PDO::PARAM_INT))
29			printf("[%03d + 2] Cannot bind integer column, %s %s\n", $offset,
30				$stmt->errorCode(), var_export($stmt->errorInfo(), true));
31
32		if (!$stmt->bindColumn(2, $label, PDO::PARAM_STR))
33			printf("[%03d + 3] Cannot bind string column, %s %s\n", $offset,
34				$stmt->errorCode(), var_export($stmt->errorInfo(), true));
35
36		while ($stmt->fetch(PDO::FETCH_BOUND))
37			printf("in = %d -> id = %s (%s) / label = %s (%s)\n",
38				$in,
39				var_export($id, true), gettype($id),
40				var_export($label, true), gettype($label));
41
42		printf("Same again...\n");
43		$stmt->execute();
44		while ($stmt->fetch(PDO::FETCH_BOUND))
45			printf("in = %d -> id = %s (%s) / label = %s (%s)\n",
46				$in,
47				var_export($id, true), gettype($id),
48				var_export($label, true), gettype($label));
49
50		// NULL values
51		printf("NULL...\n");
52		$stmt = $db->prepare('INSERT INTO test(id, label) VALUES (100, ?)');
53		$label = null;
54		if (!$stmt->bindParam(1, $label))
55			printf("[%03d + 4] Cannot bind parameter, %s %s\n", $offset,
56				$stmt->errorCode(), var_export($stmt->errorInfo(), true));
57
58		if (!$stmt->execute())
59			printf("[%03d + 5] Cannot execute statement, %s %s\n", $offset,
60				$stmt->errorCode(), var_export($stmt->errorInfo(), true));
61
62		/* NOTE: you cannot use PDO::query() with unbuffered, native PS - see extra test */
63		$stmt = $db->prepare('SELECT id, NULL AS _label FROM test WHERE label IS NULL');
64		$stmt->execute();
65
66		$id = $label = 'bogus';
67		if (!$stmt->bindColumn(1, $id, PDO::PARAM_INT))
68			printf("[%03d + 6] Cannot bind NULL column, %s %s\n", $offset,
69				$stmt->errorCode(), var_export($stmt->errorInfo(), true));
70
71		if (!$stmt->bindColumn(2, $label, PDO::PARAM_STR))
72			printf("[%03d + 3] Cannot bind string column, %s %s\n", $offset,
73				$stmt->errorCode(), var_export($stmt->errorInfo(), true));
74
75		while ($stmt->fetch(PDO::FETCH_BOUND))
76			printf("in = %d -> id = %s (%s) / label = %s (%s)\n",
77				$in,
78				var_export($id, true), gettype($id),
79				var_export($label, true), gettype($label));
80	}
81
82	try {
83		printf("Emulated PS...\n");
84		$db->setAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY, 1);
85		if (1 != $db->getAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY))
86			printf("[002] Unable to turn on emulated prepared statements\n");
87
88		printf("Buffered...\n");
89		$db->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
90		pdo_mysql_stmt_bindparam($db, 3);
91
92		printf("Unbuffered...\n");
93		MySQLPDOTest::createTestTable($db);
94		$db->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
95		pdo_mysql_stmt_bindparam($db, 4);
96
97		printf("Native PS...\n");
98		$db->setAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY, 0);
99		if (0 != $db->getAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY))
100			printf("[004] Unable to turn off emulated prepared statements\n");
101
102		printf("Buffered...\n");
103		MySQLPDOTest::createTestTable($db);
104		$db->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, true);
105		pdo_mysql_stmt_bindparam($db, 5);
106
107		printf("Unbuffered...\n");
108		MySQLPDOTest::createTestTable($db);
109		$db->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
110		pdo_mysql_stmt_bindparam($db, 6);
111
112	} catch (PDOException $e) {
113		printf("[001] %s [%s] %s\n",
114			$e->getMessage(), $db->errorCode(), implode(' ', $db->errorInfo()));
115	}
116
117	print "done!";
118?>
119--CLEAN--
120<?php
121require dirname(__FILE__) . '/mysql_pdo_test.inc';
122MySQLPDOTest::dropTestTable();
123?>
124--EXPECT--
125Emulated PS...
126Buffered...
127in = 0 -> id = 1 (integer) / label = 'a' (string)
128in = 0 -> id = 2 (integer) / label = 'b' (string)
129Same again...
130in = 0 -> id = 1 (integer) / label = 'a' (string)
131in = 0 -> id = 2 (integer) / label = 'b' (string)
132NULL...
133in = 0 -> id = 100 (integer) / label = NULL (NULL)
134Unbuffered...
135in = 0 -> id = 1 (integer) / label = 'a' (string)
136in = 0 -> id = 2 (integer) / label = 'b' (string)
137Same again...
138in = 0 -> id = 1 (integer) / label = 'a' (string)
139in = 0 -> id = 2 (integer) / label = 'b' (string)
140NULL...
141in = 0 -> id = 100 (integer) / label = NULL (NULL)
142Native PS...
143Buffered...
144in = 0 -> id = 1 (integer) / label = 'a' (string)
145in = 0 -> id = 2 (integer) / label = 'b' (string)
146Same again...
147in = 0 -> id = 1 (integer) / label = 'a' (string)
148in = 0 -> id = 2 (integer) / label = 'b' (string)
149NULL...
150in = 0 -> id = 100 (integer) / label = NULL (NULL)
151Unbuffered...
152in = 0 -> id = 1 (integer) / label = 'a' (string)
153in = 0 -> id = 2 (integer) / label = 'b' (string)
154Same again...
155in = 0 -> id = 1 (integer) / label = 'a' (string)
156in = 0 -> id = 2 (integer) / label = 'b' (string)
157NULL...
158in = 0 -> id = 100 (integer) / label = NULL (NULL)
159done!
160