2017-02-22 01:05:21 +01:00
--TEST--
2017-02-22 01:54:47 +01:00
Test fetch from binary, varbinary, varbinary(max), image columns, without setting binary encoding.
2017-02-22 02:42:39 +01:00
--DESCRIPTION--
2017-02-22 01:05:21 +01:00
Verifies GitHub issue 270 is fixed, users could not retrieve the data as inserted in binary columns without setting the binary encoding either on stmt or using bindCoulmn encoding.
2017-02-22 19:11:27 +01:00
This test verifies that the data inserted in binary columns can be retrieved using fetch, fetchColumn, fetchObject, and fetchAll functions.
2017-05-02 21:00:53 +02:00
--SKIPIF--
2017-10-04 22:16:25 +02:00
<?php require('skipif_mid-refactor.inc'); ?>
2017-02-22 01:05:21 +01:00
--FILE--
<?php
2017-10-04 22:16:25 +02:00
require_once("MsCommon_mid-refactor.inc");
2017-02-22 01:54:47 +01:00
2017-02-22 19:11:27 +01:00
$tableName = 'test_binary'.rand();
2017-10-04 22:16:25 +02:00
$columns = array('col1', 'col2', 'col3');
2017-02-22 01:05:21 +01:00
2017-10-04 22:16:25 +02:00
try {
// Connect
$conn = connect();
2017-02-22 01:05:21 +01:00
2017-10-04 22:16:25 +02:00
$colmeta_arr = array($columns[0] => "binary(50)", $columns[1] => "varbinary(50)", $columns[2] => "varbinary(max)");
$icon = base64_decode("This is some text to test retrieving from binary type columns");
$inputs = array($columns[0] => new BindParamOp(1, $icon, "PDO::PARAM_LOB", null, "PDO::SQLSRV_ENCODING_BINARY"),
$columns[1] => new BindParamOp(2, $icon, "PDO::PARAM_LOB", null, "PDO::SQLSRV_ENCODING_BINARY"),
$columns[2] => new BindParamOp(3, $icon, "PDO::PARAM_LOB", null, "PDO::SQLSRV_ENCODING_BINARY"));
2017-02-22 01:05:21 +01:00
2017-10-04 22:16:25 +02:00
if (!isColEncrypted()) {
// image is not supported for encryption
array_push($columns, 'col4');
2017-10-04 23:36:47 +02:00
$colmeta_arr += array($columns[3] => "image");
$inputs += array( $columns[3] => new BindParamOp(4, $icon, "PDO::PARAM_LOB", null, "PDO::SQLSRV_ENCODING_BINARY"));
2017-10-04 22:16:25 +02:00
}
2017-10-04 23:36:47 +02:00
2017-10-04 22:16:25 +02:00
createTable($conn, $tableName, $colmeta_arr);
// Insert data using bind parameters
insertRow($conn, $tableName, $inputs, "prepareBindParam");
2017-02-22 01:54:47 +01:00
2017-10-04 22:16:25 +02:00
// loop through each column in the table
foreach ($columns as $col) {
2017-10-04 23:36:47 +02:00
testFetch($conn, $tableName, $col, $icon);
2017-10-04 22:16:25 +02:00
}
// DROP table
dropTable($conn, $tableName);
//free statement and connection
unset($stmt);
unset($conn);
2017-02-22 01:54:47 +01:00
2017-10-04 22:16:25 +02:00
print_r("Test finished successfully\n");
} catch (PDOException $e) {
var_dump($e->errorInfo);
}
2017-02-22 01:05:21 +01:00
2021-07-29 00:45:04 +02:00
function verifyBinaryResult($result, $input, $len, $message)
{
if (PHP_VERSION_ID < 80100) {
if (strncmp($result, $input, $len) !== 0) {
print_r($message);
}
} else {
if (!feof($result)) {
$str = fread($result, $len);
if (strncmp($str, $input, $len) !== 0) {
print_r($message);
}
}
}
}
//calls various fetch methods
2017-10-04 23:36:47 +02:00
function testFetch($conn, $tableName, $columnName, $input)
2017-10-04 22:16:25 +02:00
{
2017-05-02 21:00:53 +02:00
$len = strlen($input);
$result = "";
$sql = "SELECT $columnName from $tableName";
2017-10-04 22:16:25 +02:00
$stmt = $conn->query($sql);
2017-05-02 21:00:53 +02:00
$stmt->bindColumn(1, $result, PDO::PARAM_LOB);
$stmt->fetch(PDO::FETCH_BOUND);
//binary is fixed size, to evaluate output, compare it using strncmp
2021-07-29 00:45:04 +02:00
verifyBinaryResult($result, $input, $len, "\nRetrieving using bindColumn failed");
2017-05-02 21:00:53 +02:00
$result = "";
2017-10-04 22:16:25 +02:00
$stmt = $conn->query($sql);
$stmt->bindColumn(1, $result, PDO::PARAM_LOB, 0, PDO::SQLSRV_ENCODING_BINARY);
2017-05-02 21:00:53 +02:00
$stmt->fetch(PDO::FETCH_BOUND);
2021-07-29 00:45:04 +02:00
verifyBinaryResult($result, $input, $len, "\nRetrieving using bindColumn with encoding set failed");
2017-05-02 21:00:53 +02:00
$result = "";
2017-10-04 22:16:25 +02:00
$stmt = $conn->query($sql);
2017-05-02 21:00:53 +02:00
$result = $stmt->fetchColumn();
2017-10-04 22:16:25 +02:00
if (strncmp($result, $input, $len) !== 0) {
2017-05-02 21:00:53 +02:00
print_r("\nRetrieving using fetchColumn failed");
}
$result = "";
2017-10-04 22:16:25 +02:00
$stmt = $conn->query($sql);
2017-05-02 21:00:53 +02:00
$result = $stmt->fetchObject();
2017-10-04 22:16:25 +02:00
if (strncmp($result->$columnName, $input, $len) !== 0) {
2017-05-02 21:00:53 +02:00
print_r("\nRetrieving using fetchObject failed");
}
$result = "";
2017-10-04 22:16:25 +02:00
$stmt = $conn->query($sql);
$result = $stmt->fetchAll(PDO::FETCH_COLUMN);
if (strncmp($result[0], $input, $len) !== 0) {
2017-05-02 21:00:53 +02:00
print_r("\nRetrieving using fetchAll failed");
}
2017-10-04 22:16:25 +02:00
unset($stmt);
2017-02-22 01:05:21 +01:00
}
?>
--EXPECT--
2017-02-22 18:21:44 +01:00
Test finished successfully