2017-03-09 00:53:59 +01:00
|
|
|
|
--TEST--
|
|
|
|
|
Test calling sqlsrv_get_field twice in a row. Intentionally trigger various error messages.
|
|
|
|
|
--FILE--
|
|
|
|
|
<?php
|
2017-10-04 23:22:56 +02:00
|
|
|
|
require_once('MsCommon.inc');
|
2017-03-09 00:53:59 +01:00
|
|
|
|
|
|
|
|
|
function FetchFieldTwice($conn)
|
|
|
|
|
{
|
|
|
|
|
$tableName = GetTempTableName();
|
2017-10-04 23:22:56 +02:00
|
|
|
|
|
|
|
|
|
$stmt = sqlsrv_query($conn, "CREATE TABLE $tableName ([c1_int] int, [c2_float] float, [c3_real] real, [c4_datetime] datetime)");
|
2017-03-09 00:53:59 +01:00
|
|
|
|
sqlsrv_free_stmt($stmt);
|
2017-10-04 23:22:56 +02:00
|
|
|
|
|
2017-03-09 00:53:59 +01:00
|
|
|
|
$query = "INSERT INTO $tableName ([c1_int], [c2_float], [c3_real], [c4_datetime]) VALUES ((968580013), (1.09), (3.438), ('1756-04-16 23:27:09.131'))";
|
|
|
|
|
$stmt = sqlsrv_query($conn, $query);
|
|
|
|
|
sqlsrv_execute($stmt);
|
2017-10-04 23:22:56 +02:00
|
|
|
|
PrintError(); // errors expected here
|
|
|
|
|
|
|
|
|
|
sqlsrv_free_stmt($stmt);
|
2017-03-09 00:53:59 +01:00
|
|
|
|
|
|
|
|
|
$query = "SELECT * FROM $tableName";
|
|
|
|
|
$stmt = sqlsrv_prepare($conn, $query);
|
2017-10-04 23:22:56 +02:00
|
|
|
|
$result = sqlsrv_fetch($stmt);
|
|
|
|
|
if ($result !== false) {
|
2017-03-09 00:53:59 +01:00
|
|
|
|
echo "Fetch should have failed!\n";
|
|
|
|
|
}
|
2017-10-04 23:22:56 +02:00
|
|
|
|
PrintError(); // errors expected here
|
|
|
|
|
|
|
|
|
|
if (! sqlsrv_execute($stmt)) {
|
|
|
|
|
fatalError("Errors in executing statement.\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$numFields = sqlsrv_num_fields($stmt);
|
2017-03-09 00:53:59 +01:00
|
|
|
|
$metadata = sqlsrv_field_metadata($stmt);
|
2017-10-04 23:22:56 +02:00
|
|
|
|
while ($result = sqlsrv_fetch($stmt)) {
|
|
|
|
|
for ($i = -1; $i <= $numFields; $i++) {
|
2017-05-02 02:01:30 +02:00
|
|
|
|
FetchField($stmt, $i, $numFields, false);
|
|
|
|
|
FetchField($stmt, $i, $numFields, true);
|
2017-03-09 00:53:59 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-10-04 23:22:56 +02:00
|
|
|
|
|
|
|
|
|
sqlsrv_free_stmt($stmt);
|
2017-03-09 00:53:59 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-05-02 02:01:30 +02:00
|
|
|
|
function FetchField($stmt, $idx, $numFields, $errorExpected)
|
2017-10-04 23:22:56 +02:00
|
|
|
|
{
|
|
|
|
|
if ($idx < 0 || $idx >= $numFields) {
|
|
|
|
|
$value1 = sqlsrv_get_field($stmt, $idx);
|
2017-05-02 02:01:30 +02:00
|
|
|
|
PrintError(true); // errors expected because $idx is out of bound
|
2017-10-04 23:22:56 +02:00
|
|
|
|
} else {
|
|
|
|
|
if ($idx == 3) {
|
|
|
|
|
$value1 = sqlsrv_get_field($stmt, $idx, SQLSRV_PHPTYPE_STRING(SQLSRV_ENC_CHAR));
|
|
|
|
|
} else {
|
|
|
|
|
$value1 = sqlsrv_get_field($stmt, $idx);
|
2017-03-09 00:53:59 +01:00
|
|
|
|
}
|
|
|
|
|
var_dump($value1);
|
2017-10-04 23:22:56 +02:00
|
|
|
|
|
2017-03-09 00:53:59 +01:00
|
|
|
|
PrintError($errorExpected);
|
2017-10-04 23:22:56 +02:00
|
|
|
|
}
|
2017-03-09 00:53:59 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function PrintError($errorExpected = true)
|
|
|
|
|
{
|
|
|
|
|
$errors = sqlsrv_errors(SQLSRV_ERR_ALL);
|
2017-10-04 23:22:56 +02:00
|
|
|
|
if (!empty($errors)) {
|
2017-03-09 00:53:59 +01:00
|
|
|
|
$e = $errors[0];
|
2017-10-04 23:22:56 +02:00
|
|
|
|
var_dump($e['message']);
|
|
|
|
|
} elseif ($errorExpected) {
|
2017-03-09 00:53:59 +01:00
|
|
|
|
echo "An error is expected!\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function Repro()
|
|
|
|
|
{
|
2017-10-04 23:22:56 +02:00
|
|
|
|
startTest("sqlsrv_fetch_field_twice_data_types");
|
|
|
|
|
try {
|
|
|
|
|
set_time_limit(0);
|
|
|
|
|
sqlsrv_configure('WarningsReturnAsErrors', 1);
|
2017-03-09 00:53:59 +01:00
|
|
|
|
|
2017-05-02 02:01:30 +02:00
|
|
|
|
echo "\nTest begins...\n";
|
2017-10-04 23:22:56 +02:00
|
|
|
|
|
2017-03-09 00:53:59 +01:00
|
|
|
|
// Connect
|
2017-10-04 23:22:56 +02:00
|
|
|
|
$conn = connect();
|
|
|
|
|
if (!$conn) {
|
|
|
|
|
fatalError("Could not connect.\n");
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-09 00:53:59 +01:00
|
|
|
|
FetchFieldTwice($conn);
|
2017-10-04 23:22:56 +02:00
|
|
|
|
|
|
|
|
|
sqlsrv_close($conn);
|
|
|
|
|
} catch (Exception $e) {
|
2017-03-09 00:53:59 +01:00
|
|
|
|
echo $e->getMessage();
|
|
|
|
|
}
|
|
|
|
|
echo "\nDone\n";
|
2017-10-04 23:22:56 +02:00
|
|
|
|
endTest("sqlsrv_fetch_field_twice_data_types");
|
2017-03-09 00:53:59 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Repro();
|
|
|
|
|
|
|
|
|
|
?>
|
2020-07-09 04:45:00 +02:00
|
|
|
|
--EXPECTF--
|
2017-03-09 00:53:59 +01:00
|
|
|
|
|
2017-05-02 02:01:30 +02:00
|
|
|
|
Test begins...
|
2017-03-09 00:53:59 +01:00
|
|
|
|
string(79) "A statement must be prepared with sqlsrv_prepare before calling sqlsrv_execute."
|
|
|
|
|
string(63) "The statement must be executed before results can be retrieved."
|
|
|
|
|
string(52) "An invalid parameter was passed to sqlsrv_get_field."
|
|
|
|
|
string(52) "An invalid parameter was passed to sqlsrv_get_field."
|
|
|
|
|
int(968580013)
|
|
|
|
|
bool(false)
|
|
|
|
|
string(25) "Field 0 returned no data."
|
|
|
|
|
float(1.09)
|
|
|
|
|
bool(false)
|
|
|
|
|
string(25) "Field 1 returned no data."
|
2020-07-09 04:45:00 +02:00
|
|
|
|
float(3.437999963760%s)
|
2017-03-09 00:53:59 +01:00
|
|
|
|
bool(false)
|
|
|
|
|
string(25) "Field 2 returned no data."
|
|
|
|
|
string(23) "1756-04-16 23:27:09.130"
|
|
|
|
|
bool(false)
|
|
|
|
|
string(25) "Field 3 returned no data."
|
|
|
|
|
string(52) "An invalid parameter was passed to sqlsrv_get_field."
|
|
|
|
|
string(52) "An invalid parameter was passed to sqlsrv_get_field."
|
|
|
|
|
|
|
|
|
|
Done
|
2017-05-02 02:01:30 +02:00
|
|
|
|
Test "sqlsrv_fetch_field_twice_data_types" completed successfully.
|