php-src/ext/pdo_mysql/tests/pdo_mysql_stmt_bindvalue.phpt
Johannes Schlüter be0793d2e7 MFH: Add mysqlnd support for PDO_mysql, fixes at least bug#41997,#42499,
pecl#12794, pecl#12401

# Running the tests:
# (Note: Doesn't work currnetly on HEAD, see:
#  http://news.php.net/php.qa/64378)
#
#  PDO_MYSQL_TEST_DSN  - DSN
#    For example: mysql:dbname=test;host=localhost;port=3306
#
#  PDO_MYSQL_TEST_HOST    - database host
#  PDO_MYSQL_TEST_DB      - database (schema) name
#  PDO_MYSQL_TEST_SOCKET  - database server socket
#  PDO_MYSQL_TEST_ENGINE  - storage engine to use
#  PDO_MYSQL_TEST_USER    - database user
#  PDO_MYSQL_TEST_PASS    - database user password
#  PDO_MYSQL_TEST_CHARSET - database charset
#
#  NOTE: if any of PDO_MYSQL_TEST_[HOST|DB|SOCKET|ENGINE|CHARSET] is
#  part of PDO_MYSQL_TEST_DSN, the values must match. That is, for example,
#  for PDO_MYSQL_TEST_DSN = mysql:dbname=test you MUST set PDO_MYSQL_TEST_DB=test.
2008-07-21 13:09:28 +00:00

333 lines
12 KiB
PHP

--TEST--
MySQL PDOStatement->bindValue()
--SKIPIF--
<?php
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'skipif.inc');
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
MySQLPDOTest::skip();
$db = MySQLPDOTest::factory();
?>
--FILE--
<?php
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
$db = MySQLPDOTest::factory();
MySQLPDOTest::createTestTable($db);
printf("Testing native PS...\n");
try {
$db->setAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY, 0);
if (0 != $db->getAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY))
printf("[002] Unable to turn off emulated prepared statements\n");
printf("Binding variable...\n");
$stmt = $db->prepare('SELECT id, label FROM test WHERE id > ? ORDER BY id ASC LIMIT 2');
$in = 0;
if (!$stmt->bindValue(1, $in))
printf("[003] Cannot bind value, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
$stmt->execute();
$id = $label = null;
if (!$stmt->bindColumn(1, $id, PDO::PARAM_INT))
printf("[004] Cannot bind integer column, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
if (!$stmt->bindColumn(2, $label, PDO::PARAM_STR))
printf("[005] Cannot bind string column, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
while ($stmt->fetch(PDO::FETCH_BOUND))
printf("in = %d -> id = %s (%s) / label = %s (%s)\n",
$in,
var_export($id, true), gettype($id),
var_export($label, true), gettype($label));
printf("Binding value and not variable...\n");
if (!$stmt->bindValue(1, 0))
printf("[006] Cannot bind value, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
$stmt->execute();
$id = $label = null;
if (!$stmt->bindColumn(1, $id, PDO::PARAM_INT))
printf("[007] Cannot bind integer column, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
if (!$stmt->bindColumn(2, $label, PDO::PARAM_STR))
printf("[008] Cannot bind string column, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
while ($stmt->fetch(PDO::FETCH_BOUND))
printf("in = %d -> id = %s (%s) / label = %s (%s)\n",
$in,
var_export($id, true), gettype($id),
var_export($label, true), gettype($label));
printf("Binding variable which references another variable...\n");
$in = 0;
$in_ref = &$in;
if (!$stmt->bindValue(1, $in_ref))
printf("[009] Cannot bind value, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
$stmt->execute();
$id = $label = null;
if (!$stmt->bindColumn(1, $id, PDO::PARAM_INT))
printf("[010] Cannot bind integer column, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
if (!$stmt->bindColumn(2, $label, PDO::PARAM_STR))
printf("[011] Cannot bind string column, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
while ($stmt->fetch(PDO::FETCH_BOUND))
printf("in = %d -> id = %s (%s) / label = %s (%s)\n",
$in,
var_export($id, true), gettype($id),
var_export($label, true), gettype($label));
printf("Binding a variable and a value...\n");
$stmt = $db->prepare('SELECT id, label FROM test WHERE id > ? AND id <= ? ORDER BY id ASC LIMIT 2');
$in = 0;
if (!$stmt->bindValue(1, $in))
printf("[012] Cannot bind value, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
if (!$stmt->bindValue(2, 2))
printf("[013] Cannot bind value, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
$stmt->execute();
$id = $label = null;
if (!$stmt->bindColumn(1, $id, PDO::PARAM_INT))
printf("[014] Cannot bind integer column, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
if (!$stmt->bindColumn(2, $label, PDO::PARAM_STR))
printf("[015] Cannot bind string column, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
while ($stmt->fetch(PDO::FETCH_BOUND))
printf("in = %d -> id = %s (%s) / label = %s (%s)\n",
$in,
var_export($id, true), gettype($id),
var_export($label, true), gettype($label));
printf("Binding a variable to two placeholders and changing the variable value in between the binds...\n");
// variable value change shall have no impact
$stmt = $db->prepare('SELECT id, label FROM test WHERE id > ? AND id <= ? ORDER BY id ASC LIMIT 2');
$in = 0;
if (!$stmt->bindValue(1, $in))
printf("[016] Cannot bind value, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
$in = 2;
if (!$stmt->bindValue(2, $in))
printf("[017] Cannot bind value, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
$stmt->execute();
$id = $label = null;
if (!$stmt->bindColumn(1, $id, PDO::PARAM_INT))
printf("[018] Cannot bind integer column, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
if (!$stmt->bindColumn(2, $label, PDO::PARAM_STR))
printf("[019] Cannot bind string column, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
while ($stmt->fetch(PDO::FETCH_BOUND))
printf("in = %d -> id = %s (%s) / label = %s (%s)\n",
$in,
var_export($id, true), gettype($id),
var_export($label, true), gettype($label));
} catch (PDOException $e) {
printf("[001] %s [%s] %s\n",
$e->getMessage(), $db->errorCode(), implode(' ', $db->errorInfo()));
}
printf("Testing emulated PS...\n");
try {
$db->setAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY, 1);
if (1 != $db->getAttribute(PDO::MYSQL_ATTR_DIRECT_QUERY))
printf("[002] Unable to turn on emulated prepared statements\n");
printf("Binding variable...\n");
$stmt = $db->prepare('SELECT id, label FROM test WHERE id > ? ORDER BY id ASC LIMIT 2');
$in = 0;
if (!$stmt->bindValue(1, $in))
printf("[003] Cannot bind value, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
$stmt->execute();
$id = $label = null;
if (!$stmt->bindColumn(1, $id, PDO::PARAM_INT))
printf("[004] Cannot bind integer column, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
if (!$stmt->bindColumn(2, $label, PDO::PARAM_STR))
printf("[005] Cannot bind string column, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
while ($stmt->fetch(PDO::FETCH_BOUND))
printf("in = %d -> id = %s (%s) / label = %s (%s)\n",
$in,
var_export($id, true), gettype($id),
var_export($label, true), gettype($label));
printf("Binding value and not variable...\n");
if (!$stmt->bindValue(1, 0))
printf("[006] Cannot bind value, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
$stmt->execute();
$id = $label = null;
if (!$stmt->bindColumn(1, $id, PDO::PARAM_INT))
printf("[007] Cannot bind integer column, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
if (!$stmt->bindColumn(2, $label, PDO::PARAM_STR))
printf("[008] Cannot bind string column, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
while ($stmt->fetch(PDO::FETCH_BOUND))
printf("in = %d -> id = %s (%s) / label = %s (%s)\n",
$in,
var_export($id, true), gettype($id),
var_export($label, true), gettype($label));
printf("Binding variable which references another variable...\n");
$in = 0;
$in_ref = &$in;
if (!$stmt->bindValue(1, $in_ref))
printf("[009] Cannot bind value, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
$stmt->execute();
$id = $label = null;
if (!$stmt->bindColumn(1, $id, PDO::PARAM_INT))
printf("[010] Cannot bind integer column, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
if (!$stmt->bindColumn(2, $label, PDO::PARAM_STR))
printf("[011] Cannot bind string column, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
while ($stmt->fetch(PDO::FETCH_BOUND))
printf("in = %d -> id = %s (%s) / label = %s (%s)\n",
$in,
var_export($id, true), gettype($id),
var_export($label, true), gettype($label));
printf("Binding a variable and a value...\n");
$stmt = $db->prepare('SELECT id, label FROM test WHERE id > ? AND id <= ? ORDER BY id ASC LIMIT 2');
$in = 0;
if (!$stmt->bindValue(1, $in))
printf("[012] Cannot bind value, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
if (!$stmt->bindValue(2, 2))
printf("[013] Cannot bind value, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
$stmt->execute();
$id = $label = null;
if (!$stmt->bindColumn(1, $id, PDO::PARAM_INT))
printf("[014] Cannot bind integer column, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
if (!$stmt->bindColumn(2, $label, PDO::PARAM_STR))
printf("[015] Cannot bind string column, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
while ($stmt->fetch(PDO::FETCH_BOUND))
printf("in = %d -> id = %s (%s) / label = %s (%s)\n",
$in,
var_export($id, true), gettype($id),
var_export($label, true), gettype($label));
printf("Binding a variable to two placeholders and changing the variable value in between the binds...\n");
// variable value change shall have no impact
$stmt = $db->prepare('SELECT id, label FROM test WHERE id > ? AND id <= ? ORDER BY id ASC LIMIT 2');
$in = 0;
if (!$stmt->bindValue(1, $in))
printf("[016] Cannot bind value, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
$in = 2;
if (!$stmt->bindValue(2, $in))
printf("[017] Cannot bind value, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
$stmt->execute();
$id = $label = null;
if (!$stmt->bindColumn(1, $id, PDO::PARAM_INT))
printf("[018] Cannot bind integer column, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
if (!$stmt->bindColumn(2, $label, PDO::PARAM_STR))
printf("[019] Cannot bind string column, %s %s\n",
$stmt->errorCode(), var_export($stmt->errorInfo(), true));
while ($stmt->fetch(PDO::FETCH_BOUND))
printf("in = %d -> id = %s (%s) / label = %s (%s)\n",
$in,
var_export($id, true), gettype($id),
var_export($label, true), gettype($label));
} catch (PDOException $e) {
printf("[001] %s [%s] %s\n",
$e->getMessage(), $db->errorCode(), implode(' ', $db->errorInfo()));
}
$db->exec('DROP TABLE IF EXISTS test');
print "done!";
?>
--EXPECTF--
Testing native PS...
Binding variable...
in = 0 -> id = 1 (integer) / label = 'a' (string)
in = 0 -> id = 2 (integer) / label = 'b' (string)
Binding value and not variable...
in = 0 -> id = 1 (integer) / label = 'a' (string)
in = 0 -> id = 2 (integer) / label = 'b' (string)
Binding variable which references another variable...
in = 0 -> id = 1 (integer) / label = 'a' (string)
in = 0 -> id = 2 (integer) / label = 'b' (string)
Binding a variable and a value...
in = 0 -> id = 1 (integer) / label = 'a' (string)
in = 0 -> id = 2 (integer) / label = 'b' (string)
Binding a variable to two placeholders and changing the variable value in between the binds...
in = 2 -> id = 1 (integer) / label = 'a' (string)
in = 2 -> id = 2 (integer) / label = 'b' (string)
Testing emulated PS...
Binding variable...
in = 0 -> id = 1 (integer) / label = 'a' (string)
in = 0 -> id = 2 (integer) / label = 'b' (string)
Binding value and not variable...
in = 0 -> id = 1 (integer) / label = 'a' (string)
in = 0 -> id = 2 (integer) / label = 'b' (string)
Binding variable which references another variable...
in = 0 -> id = 1 (integer) / label = 'a' (string)
in = 0 -> id = 2 (integer) / label = 'b' (string)
Binding a variable and a value...
in = 0 -> id = 1 (integer) / label = 'a' (string)
in = 0 -> id = 2 (integer) / label = 'b' (string)
Binding a variable to two placeholders and changing the variable value in between the binds...
in = 2 -> id = 1 (integer) / label = 'a' (string)
in = 2 -> id = 2 (integer) / label = 'b' (string)
done!