php-sqlsrv/test/pdo_sqlsrv/pdo_nested_query_mars.phpt
2017-05-04 08:14:03 -07:00

109 lines
2.6 KiB
PHP

--TEST--
fetch multiple result sets with MARS on and then off
--SKIPIF--
<?php require('skipif.inc'); ?>
--FILE--
<?php
include 'MsCommon.inc';
function NestedQuery_Mars($on)
{
require("MsSetup.inc");
$tableName = GetTempTableName();
$conn = new PDO( "sqlsrv:server=$server;database=$databaseName;MultipleActiveResultSets=$on", $uid, $pwd);
$conn->SetAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->exec("CREATE TABLE $tableName ([c1_int] int, [c2_varchar] varchar(20))");
$query = "INSERT INTO $tableName ([c1_int], [c2_varchar]) VALUES (1, 'Dummy value 1')";
$stmt = $conn->query($query);
$query = "INSERT INTO $tableName ([c1_int], [c2_varchar]) VALUES (2, 'Dummy value 2')";
$stmt = $conn->query($query);
$query = "SELECT * FROM $tableName ORDER BY [c1_int]";
$stmt = $conn->query($query);
$numRows = 0;
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
$numRows++;
if ($numRows !== 2) echo "Number of rows is unexpected!\n";
$stmt = null;
// more than one active results
$stmt1 = $conn->query($query);
$stmt2 = $conn->prepare($query);
$stmt2->execute();
echo "\nNumber of columns in First set: " . $stmt2->columnCount() . "\n";
while ($row = $stmt1->fetch(PDO::FETCH_ASSOC))
{
print_r($row);
}
echo "\nNumber of columns in Second set: " . $stmt1->columnCount() . "\n\n";
while ($row = $stmt2->fetch(PDO::FETCH_OBJ))
{
print_r($row);
}
$stmt1 = null;
$stmt2 = null;
$conn = null;
}
function RunTest()
{
StartTest("pdo_nested_query_mars");
echo "\nStarting test...\n";
try
{
NestedQuery_Mars(true);
NestedQuery_Mars(false);
}
catch (Exception $e)
{
echo $e->getMessage();
}
echo "\nDone\n";
EndTest("pdo_nested_query_mars");
}
RunTest();
?>
--EXPECT--
Starting test...
Number of columns in First set: 2
Array
(
[c1_int] => 1
[c2_varchar] => Dummy value 1
)
Array
(
[c1_int] => 2
[c2_varchar] => Dummy value 2
)
Number of columns in Second set: 2
stdClass Object
(
[c1_int] => 1
[c2_varchar] => Dummy value 1
)
stdClass Object
(
[c1_int] => 2
[c2_varchar] => Dummy value 2
)
SQLSTATE[IMSSP]: The connection cannot process this operation because there is a statement with pending results. To make the connection available for other queries, either fetch all results or cancel or free the statement. For more information, see the product documentation about the MultipleActiveResultSets connection option.
Done
Test "pdo_nested_query_mars" completed successfully.