B
B
Barmunk2017-09-22 17:14:19
PHP
Barmunk, 2017-09-22 17:14:19

How to send binary data using pdo_dblib?

php 5.6 \ pdo_dblib \ freetds
If this was a SQLSRV Windows driver, then using the PDO::SQLSRV_ENCODING_BINARY constant, the type is quite successfully specified and the data is sent.

$stmt = $conn->prepare("EXEC proc @fileData = ?");
$stmt->bindParam(1, $fileData, PDO::PARAM_LOB, 0, PDO::SQLSRV_ENCODING_BINARY);
$stmt->execute();

but on Linux there is a dblib driver that does not have such a constant (moving to another is not an option). I tried to use bin2hex, and then pass a string that is already converted on the sql side into a binary type, but this only works on files less than one meter, then there are losses and too long conversion.
Found a pull request with new types (DBLIB_PARAM_BINARY), but a year has passed https://github.com/php/php-src/pull/2168/commits/2...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Barmunk, 2017-09-27
@Barmunk

found a way for BINARY type

$hex = mssql_escape($data);
$sql = "SET TEXTSIZE 524288000; EXEC proc @fileData = $hex";

function mssql_escape($data) 
{
   if(is_numeric($data)) return $data;

   $unpacked = unpack('H*hex', $data);
   return '0x' . $unpacked['hex'];
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question