php-sqlsrv/test/functional/sqlsrv/TC44_FetchArray.phpt

190 lines
5.4 KiB
Plaintext
Raw Normal View History

2017-05-04 01:00:31 +02:00
--TEST--
Fetch Array Test
--DESCRIPTION--
2017-10-11 01:54:20 +02:00
Verifies data retrieval via "sqlsrv_fetch_array",
2017-05-04 01:00:31 +02:00
by checking all fetch type modes.
--ENV--
PHPT_EXEC=true
--SKIPIF--
<?php require('skipif_versions_old.inc'); ?>
2017-05-04 01:00:31 +02:00
--FILE--
<?php
require_once('MsCommon.inc');
2017-05-04 01:00:31 +02:00
2017-10-11 01:54:20 +02:00
function fetchRow($minFetchMode, $maxFetchMode)
2017-05-04 01:00:31 +02:00
{
if (!isMarsSupported()) {
2017-05-04 01:00:31 +02:00
return;
}
setup();
$tableName = 'TC44test';
if (useUTF8Data()) {
$conn1 = AE\connect(array( 'CharacterSet'=>'UTF-8' ));
} else {
$conn1 = AE\connect();
}
2017-10-11 01:54:20 +02:00
$tableName = 'TC44Test';
AE\createTestTable($conn1, $tableName);
2017-05-04 01:00:31 +02:00
$noRows = 10;
$numFields = 0;
AE\insertTestRows($conn1, $tableName, $noRows);
for ($k = $minFetchMode; $k <= $maxFetchMode; $k++) {
$stmt1 = AE\selectFromTable($conn1, $tableName);
$stmt2 = AE\selectFromTable($conn1, $tableName);
if ($numFields == 0) {
2017-05-04 01:00:31 +02:00
$numFields = sqlsrv_num_fields($stmt1);
} else {
2017-05-04 01:00:31 +02:00
$count = sqlsrv_num_fields($stmt1);
if ($count != $numFields) {
2017-05-04 01:00:31 +02:00
die("Unexpected number of fields: $count");
}
}
switch ($k) {
case 1: // fetch array - numeric mode
2017-10-11 01:54:20 +02:00
fetchArray($stmt1, $stmt2, SQLSRV_FETCH_NUMERIC, $noRows, $numFields);
2017-05-04 01:00:31 +02:00
break;
case 2: // fetch array - associative mode
2017-10-11 01:54:20 +02:00
fetchArray($stmt1, $stmt2, SQLSRV_FETCH_ASSOC, $noRows, $numFields);
2017-05-04 01:00:31 +02:00
break;
case 3: // fetch array - both numeric & associative
2017-10-11 01:54:20 +02:00
fetchArray($stmt1, $stmt2, SQLSRV_FETCH_BOTH, $noRows, $numFields);
2017-05-04 01:00:31 +02:00
break;
default: // default
2017-10-11 01:54:20 +02:00
fetchArray($stmt1, $stmt2, null, $noRows, $numFields);
2017-05-04 01:00:31 +02:00
break;
}
sqlsrv_free_stmt($stmt1);
sqlsrv_free_stmt($stmt2);
}
dropTable($conn1, $tableName);
2017-05-04 01:00:31 +02:00
sqlsrv_close($conn1);
}
2017-10-11 01:54:20 +02:00
function fetchArray($stmt, $stmtRef, $mode, $rows, $fields)
2017-05-04 01:00:31 +02:00
{
$size = $fields;
$fetchMode = $mode;
if ($fetchMode == SQLSRV_FETCH_NUMERIC) {
trace("\tRetrieving $rows arrays of size $size (Fetch Mode = NUMERIC) ...\n");
} elseif ($fetchMode == SQLSRV_FETCH_ASSOC) {
trace("\tRetrieving $rows arrays of size $size (Fetch Mode = ASSOCIATIVE) ...\n");
} elseif ($fetchMode == SQLSRV_FETCH_BOTH) {
2017-05-04 01:00:31 +02:00
$size = $fields * 2;
trace("\tRetrieving $rows arrays of size $size (Fetch Mode = BOTH) ...\n");
} else {
2017-05-04 01:00:31 +02:00
$fetchMode = null;
$size = $fields * 2;
trace("\tRetrieving $rows arrays of size $size (Fetch Mode = DEFAULT) ...\n");
2017-05-04 01:00:31 +02:00
}
for ($i = 0; $i < $rows; $i++) {
if ($fetchMode == null) {
2017-05-04 01:00:31 +02:00
$row = sqlsrv_fetch_array($stmt);
} else {
2017-05-04 01:00:31 +02:00
$row = sqlsrv_fetch_array($stmt, $fetchMode);
}
if ($row === false) {
fatalError("Row $i is missing");
2017-05-04 01:00:31 +02:00
}
$rowSize = count($row);
if ($rowSize != $size) {
2017-05-04 01:00:31 +02:00
die("Row array has an incorrect size: ".$rowSize);
}
$rowRref = sqlsrv_fetch($stmtRef);
for ($j = 0; $j < $fields; $j++) {
2017-10-11 01:54:20 +02:00
if (!checkData($row, $stmtRef, $j, $fetchMode)) {
2017-05-04 01:00:31 +02:00
die("Data corruption on row ".($i + 1)." column ".($j + 1));
}
}
}
}
2017-10-11 01:54:20 +02:00
function checkData($row, $stmt, $index, $mode)
2017-05-04 01:00:31 +02:00
{
$success = true;
$col = $index + 1;
$actual = (($mode == SQLSRV_FETCH_ASSOC) ? $row[getColName($col)] : $row[$index]);
2017-05-04 01:00:31 +02:00
$expected = null;
if (!isUpdatable($col)) {
2017-05-04 01:00:31 +02:00
// do not check the timestamp
} elseif (isNumeric($col) || isDateTime($col)) {
2017-05-04 01:00:31 +02:00
$expected = sqlsrv_get_field($stmt, $index);
if ($expected != $actual) {
2017-05-04 01:00:31 +02:00
$success = false;
}
} elseif (isBinary($col)) {
2017-05-04 01:00:31 +02:00
$expected = sqlsrv_get_field($stmt, $index, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR));
2021-07-29 00:45:04 +02:00
if (is_null($expected)) {
$success = is_null($actual);
} else {
if (is_null($actual)) {
$success = false;
} else {
$actual = bin2hex($actual);
if (strcasecmp($actual, $expected) != 0) {
$success = false;
}
}
2017-05-04 01:00:31 +02:00
}
} else { // if (isChar($col))
if (useUTF8Data()) {
2017-05-04 01:00:31 +02:00
$expected = sqlsrv_get_field($stmt, $index, SQLSRV_PHPTYPE_STRING('UTF-8'));
} else {
2017-05-04 01:00:31 +02:00
$expected = sqlsrv_get_field($stmt, $index, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR));
}
2021-07-29 00:45:04 +02:00
if (is_null($expected)) {
$success = is_null($actual);
} elseif (strcmp($actual, $expected) != 0) {
2017-05-04 01:00:31 +02:00
$success = false;
}
}
if (!$success) {
trace("\nData error\nExpected:\n$expected\nActual:\n$actual\n");
2017-05-04 01:00:31 +02:00
}
return ($success);
}
// locale must be set before 1st connection
2018-01-02 23:42:08 +01:00
setUSAnsiLocale();
$testName = "Fetch - Array";
// test ansi only if windows or non-UTF8 locales are supported (ODBC 17 and above)
startTest($testName);
if (isLocaleSupported()) {
try {
setUTF8Data(false);
fetchRow(1, 4);
} catch (Exception $e) {
echo $e->getMessage();
}
}
endTest($testName);
// test utf8
startTest($testName);
2017-10-11 01:54:20 +02:00
try {
setUTF8Data(true);
2018-01-04 17:53:20 +01:00
resetLocaleToDefault();
2017-10-11 01:54:20 +02:00
fetchRow(1, 4);
} catch (Exception $e) {
echo $e->getMessage();
}
endTest($testName);
2017-05-04 01:00:31 +02:00
?>
--EXPECT--
Test "Fetch - Array" completed successfully.
Test "Fetch - Array" completed successfully.