2017-05-03 23:09:02 +03:00
|
|
|
--TEST--
|
|
|
|
PDO Fetch Bound Test
|
|
|
|
--DESCRIPTION--
|
|
|
|
Verification for "PDOStatenent::fetch(PDO::FETCH_BOUND)".
|
|
|
|
--ENV--
|
|
|
|
PHPT_EXEC=true
|
|
|
|
--SKIPIF--
|
2017-12-01 01:37:25 +03:00
|
|
|
<?php require_once('skipif_mid-refactor.inc'); ?>
|
2017-05-03 23:09:02 +03:00
|
|
|
--FILE--
|
|
|
|
<?php
|
2017-12-01 01:37:25 +03:00
|
|
|
require_once("MsCommon_mid-refactor.inc");
|
2017-05-03 23:09:02 +03:00
|
|
|
|
2017-12-01 01:37:25 +03:00
|
|
|
try {
|
|
|
|
$conn1 = connect();
|
2017-05-03 23:09:02 +03:00
|
|
|
|
|
|
|
// Prepare test table
|
2017-12-01 01:37:25 +03:00
|
|
|
$tableName = "php_test_table";
|
|
|
|
createTable($conn1, $tableName, array(new ColumnMeta("int", "id", "NOT NULL PRIMARY KEY"), "val" => "varchar(10)", "val2" => "varchar(16)"));
|
|
|
|
$data = array(array('10', 'Abc', 'zxy'),
|
|
|
|
array('20', 'Def', 'wvu'),
|
|
|
|
array('30', 'Ghi', 'tsr'),
|
|
|
|
array('40', 'Jkl', 'qpo'),
|
|
|
|
array('50', 'Mno', 'nml'),
|
|
|
|
array('60', 'Pqr', 'kji'));
|
2017-05-03 23:09:02 +03:00
|
|
|
|
|
|
|
// Insert using question mark placeholders
|
2017-12-06 03:32:18 +03:00
|
|
|
$stmt1 = $conn1->prepare("INSERT INTO [$tableName] VALUES(?, ?, ?)");
|
2017-12-01 01:37:25 +03:00
|
|
|
foreach ($data as $row) {
|
2017-05-03 23:09:02 +03:00
|
|
|
$stmt1->execute($row);
|
|
|
|
}
|
|
|
|
unset($stmt1);
|
|
|
|
|
|
|
|
// Count inserted rows
|
2017-12-06 03:32:18 +03:00
|
|
|
$stmt2 = $conn1->prepare("SELECT COUNT(id) FROM [$tableName]");
|
2017-05-03 23:09:02 +03:00
|
|
|
$stmt2->execute();
|
|
|
|
$num = $stmt2->fetchColumn();
|
2017-12-01 01:37:25 +03:00
|
|
|
echo "There are $num rows in the table.\n";
|
2017-05-03 23:09:02 +03:00
|
|
|
$stmt2->closeCursor();
|
|
|
|
|
|
|
|
// Insert using named parameters
|
2017-12-01 01:37:25 +03:00
|
|
|
$stmt1 = $conn1->prepare("INSERT INTO [$tableName] VALUES(:first, :second, :third)");
|
|
|
|
foreach ($data as $row) {
|
2017-05-03 23:09:02 +03:00
|
|
|
$stmt1->execute(array(':first'=>($row[0] + 5), ':second'=>$row[1], ':third'=>$row[2]));
|
|
|
|
}
|
|
|
|
unset($stmt1);
|
|
|
|
|
|
|
|
$stmt2->execute();
|
|
|
|
$num = $stmt2->fetchColumn();
|
2017-12-01 01:37:25 +03:00
|
|
|
echo "There are $num rows in the table.\n";
|
2017-05-03 23:09:02 +03:00
|
|
|
|
2017-12-08 23:11:53 +03:00
|
|
|
createTable($conn1, $tableName, array(new ColumnMeta("int", "idx", "NOT NULL PRIMARY KEY", "none"), "txt" => "varchar(20)"));
|
2017-12-01 01:37:25 +03:00
|
|
|
insertRow($conn1, $tableName, array("idx" => 0, "txt" => 'String0'));
|
|
|
|
insertRow($conn1, $tableName, array("idx" => 1, "txt" => 'String1'));
|
|
|
|
insertRow($conn1, $tableName, array("idx" => 2, "txt" => 'String2'));
|
2017-05-03 23:09:02 +03:00
|
|
|
|
|
|
|
// Testing with prepared query
|
2017-12-01 01:37:25 +03:00
|
|
|
$stmt1 = $conn1->prepare("SELECT COUNT(idx) FROM [$tableName]");
|
2017-05-03 23:09:02 +03:00
|
|
|
$stmt1->execute();
|
|
|
|
var_dump($stmt1->fetchColumn());
|
|
|
|
unset($stmt1);
|
|
|
|
|
2017-12-06 22:54:10 +03:00
|
|
|
$stmt1 = $conn1->prepare("SELECT idx, txt FROM [$tableName] ORDER BY idx");
|
2017-05-03 23:09:02 +03:00
|
|
|
$stmt1->execute();
|
|
|
|
$data = $stmt1->fetchAll(PDO::FETCH_COLUMN|PDO::FETCH_UNIQUE);
|
|
|
|
var_dump($data);
|
|
|
|
|
|
|
|
echo "===WHILE===\n";
|
|
|
|
|
|
|
|
$stmt1->bindColumn('idx', $idx);
|
|
|
|
$stmt1->bindColumn('txt', $txt);
|
|
|
|
$stmt1->execute();
|
2017-12-01 01:37:25 +03:00
|
|
|
while ($stmt1->fetch(PDO::FETCH_BOUND)) {
|
2017-12-08 01:10:10 +03:00
|
|
|
var_dump(array($idx=>$txt));
|
2017-05-03 23:09:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
echo "===ALONE===\n";
|
|
|
|
|
2017-12-01 01:37:25 +03:00
|
|
|
$stmt2 = $conn1->prepare("SELECT txt FROM [$tableName] WHERE idx=:inp");
|
2017-05-03 23:09:02 +03:00
|
|
|
$stmt2->bindParam(':inp', $idx); // by foreign name
|
|
|
|
|
2017-12-01 01:37:25 +03:00
|
|
|
$stmt3 = $conn1->prepare("SELECT idx FROM [$tableName] WHERE txt=:txt");
|
2017-05-03 23:09:02 +03:00
|
|
|
$stmt3->bindParam(':txt', $txt); // using same name
|
|
|
|
|
2017-12-01 01:37:25 +03:00
|
|
|
foreach ($data as $idx => $txt) {
|
2017-05-03 23:09:02 +03:00
|
|
|
var_dump(array($idx=>$txt));
|
|
|
|
|
|
|
|
var_dump($stmt2->execute());
|
2017-12-01 01:37:25 +03:00
|
|
|
if ($idx == 0) { // bindColumn()s after execute() has been called at least once
|
2017-12-06 22:54:10 +03:00
|
|
|
$stmt2->bindColumn('txt', $col);
|
2017-05-03 23:09:02 +03:00
|
|
|
}
|
|
|
|
var_dump($stmt2->fetch(PDO::FETCH_BOUND));
|
|
|
|
$stmt2->closeCursor();
|
|
|
|
|
|
|
|
var_dump($stmt3->execute());
|
2017-12-01 01:37:25 +03:00
|
|
|
if ($idx == 0) { // bindColumn()s after execute() has been called at least once
|
2017-12-06 03:32:18 +03:00
|
|
|
$stmt3->bindColumn('idx', $idxCol);
|
2017-05-03 23:09:02 +03:00
|
|
|
}
|
|
|
|
var_dump($stmt3->fetch(PDO::FETCH_BOUND));
|
|
|
|
$stmt3->closeCursor();
|
|
|
|
|
2017-12-06 22:54:10 +03:00
|
|
|
var_dump(array($idxCol=>$col));
|
2017-05-03 23:09:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
echo "===REBIND/SAME===\n";
|
|
|
|
|
2017-12-06 22:54:10 +03:00
|
|
|
$stmt3->bindColumn('idx', $col);
|
2017-12-01 01:37:25 +03:00
|
|
|
foreach ($data as $idx => $txt) {
|
2017-05-03 23:09:02 +03:00
|
|
|
var_dump(array($idx=>$txt));
|
|
|
|
var_dump($stmt2->execute());
|
|
|
|
var_dump($stmt2->fetch(PDO::FETCH_BOUND));
|
|
|
|
$stmt2->closeCursor();
|
|
|
|
|
2017-12-06 22:54:10 +03:00
|
|
|
var_dump($col);
|
2017-05-03 23:09:02 +03:00
|
|
|
var_dump($stmt3->execute());
|
|
|
|
var_dump($stmt3->fetch(PDO::FETCH_BOUND));
|
|
|
|
$stmt3->closeCursor();
|
2017-12-06 22:54:10 +03:00
|
|
|
var_dump($col);
|
2017-05-03 23:09:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
echo "===REBIND/CONFLICT===\n";
|
|
|
|
|
2017-12-06 03:32:18 +03:00
|
|
|
$stmt1->bindColumn('idx', $col);
|
|
|
|
$stmt1->bindColumn('txt', $col);
|
2017-05-03 23:09:02 +03:00
|
|
|
$stmt1->execute();
|
2017-12-01 01:37:25 +03:00
|
|
|
while ($stmt1->fetch(PDO::FETCH_BOUND)) {
|
2017-12-08 01:10:10 +03:00
|
|
|
var_dump($col);
|
2017-12-01 01:37:25 +03:00
|
|
|
}
|
2017-05-03 23:09:02 +03:00
|
|
|
|
|
|
|
// Cleanup
|
2017-12-01 01:37:25 +03:00
|
|
|
dropTable($conn1, $tableName);
|
|
|
|
unset($stmt1);
|
|
|
|
unset($stmt2);
|
|
|
|
unset($stmt3);
|
|
|
|
unset($conn1);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
echo $e->getMessage();
|
2017-05-03 23:09:02 +03:00
|
|
|
}
|
|
|
|
?>
|
|
|
|
--EXPECT--
|
|
|
|
There are 6 rows in the table.
|
|
|
|
There are 12 rows in the table.
|
|
|
|
string(1) "3"
|
|
|
|
array(3) {
|
|
|
|
[0]=>
|
|
|
|
string(7) "String0"
|
|
|
|
[1]=>
|
|
|
|
string(7) "String1"
|
|
|
|
[2]=>
|
|
|
|
string(7) "String2"
|
|
|
|
}
|
|
|
|
===WHILE===
|
2017-12-08 01:10:10 +03:00
|
|
|
array(1) {
|
2017-05-03 23:09:02 +03:00
|
|
|
[0]=>
|
|
|
|
string(7) "String0"
|
2017-12-08 01:10:10 +03:00
|
|
|
}
|
|
|
|
array(1) {
|
2017-05-03 23:09:02 +03:00
|
|
|
[1]=>
|
|
|
|
string(7) "String1"
|
2017-12-08 01:10:10 +03:00
|
|
|
}
|
|
|
|
array(1) {
|
2017-05-03 23:09:02 +03:00
|
|
|
[2]=>
|
|
|
|
string(7) "String2"
|
|
|
|
}
|
|
|
|
===ALONE===
|
|
|
|
array(1) {
|
|
|
|
[0]=>
|
|
|
|
string(7) "String0"
|
|
|
|
}
|
|
|
|
bool(true)
|
|
|
|
bool(true)
|
|
|
|
bool(true)
|
|
|
|
bool(true)
|
|
|
|
array(1) {
|
|
|
|
[0]=>
|
|
|
|
string(7) "String0"
|
|
|
|
}
|
|
|
|
array(1) {
|
|
|
|
[1]=>
|
|
|
|
string(7) "String1"
|
|
|
|
}
|
|
|
|
bool(true)
|
|
|
|
bool(true)
|
|
|
|
bool(true)
|
|
|
|
bool(true)
|
|
|
|
array(1) {
|
|
|
|
[1]=>
|
|
|
|
string(7) "String1"
|
|
|
|
}
|
|
|
|
array(1) {
|
|
|
|
[2]=>
|
|
|
|
string(7) "String2"
|
|
|
|
}
|
|
|
|
bool(true)
|
|
|
|
bool(true)
|
|
|
|
bool(true)
|
|
|
|
bool(true)
|
|
|
|
array(1) {
|
|
|
|
[2]=>
|
|
|
|
string(7) "String2"
|
|
|
|
}
|
|
|
|
===REBIND/SAME===
|
|
|
|
array(1) {
|
|
|
|
[0]=>
|
|
|
|
string(7) "String0"
|
|
|
|
}
|
|
|
|
bool(true)
|
|
|
|
bool(true)
|
|
|
|
string(7) "String0"
|
|
|
|
bool(true)
|
|
|
|
bool(true)
|
|
|
|
string(1) "0"
|
|
|
|
array(1) {
|
|
|
|
[1]=>
|
|
|
|
string(7) "String1"
|
|
|
|
}
|
|
|
|
bool(true)
|
|
|
|
bool(true)
|
|
|
|
string(7) "String1"
|
|
|
|
bool(true)
|
|
|
|
bool(true)
|
|
|
|
string(1) "1"
|
|
|
|
array(1) {
|
|
|
|
[2]=>
|
|
|
|
string(7) "String2"
|
|
|
|
}
|
|
|
|
bool(true)
|
|
|
|
bool(true)
|
|
|
|
string(7) "String2"
|
|
|
|
bool(true)
|
|
|
|
bool(true)
|
|
|
|
string(1) "2"
|
|
|
|
===REBIND/CONFLICT===
|
2017-12-08 01:10:10 +03:00
|
|
|
string(7) "String0"
|
|
|
|
string(7) "String1"
|
|
|
|
string(7) "String2"
|