simplified the test without image input
This commit is contained in:
parent
7ca80918ef
commit
bdbfaa7c65
Binary file not shown.
Before Width: | Height: | Size: 3.8 KiB |
|
@ -3,87 +3,48 @@ GitHub Issue #35 binary encoding error when binding by name
|
||||||
--SKIPIF--
|
--SKIPIF--
|
||||||
--FILE--
|
--FILE--
|
||||||
<?php
|
<?php
|
||||||
function CompareBinaryData($inputFile, $data)
|
|
||||||
{
|
|
||||||
// open input file first
|
|
||||||
$stream = fopen($inputFile, "rb");
|
|
||||||
|
|
||||||
$len = strlen($data);
|
|
||||||
$matched = true;
|
|
||||||
$numbytes = 8192;
|
|
||||||
|
|
||||||
$pos = 0;
|
|
||||||
while (! feof($stream) && $pos < $len)
|
|
||||||
{
|
|
||||||
$contents = fread($stream, $numbytes);
|
|
||||||
|
|
||||||
// if $data is empty, check if $contents is also empty
|
|
||||||
$contents_len = strlen($contents);
|
|
||||||
if ($len == 0)
|
|
||||||
{
|
|
||||||
$matched = ($contents_len == 0);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Compare contents (case-sensitive)
|
|
||||||
$count = ($contents_len < $numbytes) ? $contents_len : $numbytes;
|
|
||||||
$result = substr_compare($data, $contents, $pos, $count);
|
|
||||||
|
|
||||||
if ($result != 0)
|
|
||||||
{
|
|
||||||
$matched = false;
|
|
||||||
echo "Data corruption!!\nExpected: $contents\nActual:" . substr($data, $pos, $count) . "\n";
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
$pos += $count;
|
|
||||||
}
|
|
||||||
|
|
||||||
// close the data stream
|
|
||||||
fclose($stream);
|
|
||||||
|
|
||||||
return $matched;
|
|
||||||
}
|
|
||||||
|
|
||||||
function test()
|
function test()
|
||||||
{
|
{
|
||||||
require_once("autonomous_setup.php");
|
require_once("autonomous_setup.php");
|
||||||
|
|
||||||
// Connect
|
// Connect
|
||||||
$conn = new PDO("sqlsrv:server=$serverName", $username, $password);
|
$dbName = "tempdb";
|
||||||
|
$conn = new PDO( "sqlsrv:server=$serverName ; database=$dbName", $username, $password);
|
||||||
|
|
||||||
// Create a temp table
|
// Create a temp table
|
||||||
$tableName = "#testTableIssue35";
|
$number = rand(0,1000);
|
||||||
$sql = "CREATE TABLE $tableName (Picture varbinary(max))";
|
$tableName = "testTableIssue35" . "_" . $number;
|
||||||
|
$sql = "CREATE TABLE $tableName (Value varbinary(max))";
|
||||||
$stmt = $conn->query($sql);
|
$stmt = $conn->query($sql);
|
||||||
|
|
||||||
// Insert data using bind parameters
|
// Insert data using bind parameters
|
||||||
$sql = "INSERT INTO $tableName VALUES (?)";
|
$sql = "INSERT INTO $tableName VALUES (?)";
|
||||||
$stmt = $conn->prepare($sql);
|
$stmt = $conn->prepare($sql);
|
||||||
$file = dirname(__FILE__)."/bike.jpg";
|
$message = "This is to test github issue 35.";
|
||||||
$stream = fopen($file, "rb");
|
$value = base64_encode($message);
|
||||||
|
|
||||||
$stmt->setAttribute(constant('PDO::SQLSRV_ATTR_ENCODING'), PDO::SQLSRV_ENCODING_BINARY);
|
$stmt->setAttribute(constant('PDO::SQLSRV_ATTR_ENCODING'), PDO::SQLSRV_ENCODING_BINARY);
|
||||||
$stmt->bindParam(1, $stream, PDO::PARAM_LOB);
|
$stmt->bindParam(1, $value, PDO::PARAM_LOB);
|
||||||
$result = $stmt->execute();
|
$result = $stmt->execute();
|
||||||
fclose($stream);
|
|
||||||
|
|
||||||
// fetch it back
|
// fetch it back
|
||||||
$stmt = $conn->prepare("SELECT Picture FROM $tableName");
|
$stmt = $conn->prepare("SELECT Value FROM $tableName");
|
||||||
|
$stmt->bindColumn('Value', $val1, PDO::PARAM_LOB, 0, PDO::SQLSRV_ENCODING_BINARY);
|
||||||
$stmt->execute();
|
$stmt->execute();
|
||||||
$stmt->bindColumn('Picture', $image, PDO::PARAM_LOB, 0, PDO::SQLSRV_ENCODING_BINARY);
|
|
||||||
$stmt->fetch(PDO::FETCH_BOUND);
|
$stmt->fetch(PDO::FETCH_BOUND);
|
||||||
|
var_dump($val1 === $value);
|
||||||
|
|
||||||
var_dump(CompareBinaryData($file, $image));
|
$stmt = $conn->query("DROP TABLE $tableName");
|
||||||
|
|
||||||
// Close connection
|
// Close connection
|
||||||
$stmt = null;
|
$stmt = null;
|
||||||
$conn = null;
|
$conn = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
test();
|
test();
|
||||||
|
|
||||||
print "Done";
|
print "Done";
|
||||||
?>
|
?>
|
||||||
--EXPECT--
|
--EXPECT--
|
||||||
bool(true)
|
bool(true)
|
||||||
Done
|
Done
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue