Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Linux] Fetching VARBINARY value with pdo_sqlsrv does not work #270

Closed
deeky666 opened this issue Feb 2, 2017 · 5 comments
Closed

[Linux] Fetching VARBINARY value with pdo_sqlsrv does not work #270

deeky666 opened this issue Feb 2, 2017 · 5 comments

Comments

@deeky666
Copy link

deeky666 commented Feb 2, 2017

I am not quite sure if this is a bug or I just cannot figure out how to do this. Fetching a value from a VARBINARY column does not return the same value that was inserted before when using pdo_sqlsrv. Expected result would be the binary representation that was inserted into the database but instead the hexadecimal representation is returned.

Test

$sql = "CREATE TABLE foo (icon VARBINARY(MAX))";
$conn->exec($sql);

$icon = base64_decode(<<<EOF
H4sICJRACVgCA2RvY3RyaW5lLmljbwDtVNtLFHEU/ia1i9fVzVWxvJSrZmoXS6pd0zK7QhdNc03z
lrpppq1pWqJCFERZkUFEDybYBQqJhB6iUOqhh+whgl4qkF6MfGh+s87O7GVmO6OlBfUfdIZvznxn
fpzznW9gAI4unQ50XwirH2AAkEygEuIwU58ODnPBzXGv14sEq4BrwzKKL4sY++SGTz6PodcutN5x
IPvsFCa+K9CXMfS/cOL5OxesN0Wceygho0WAXVLwcUJBdDVDaqOAij4Rrz640XlXQmAxQ16PHU63
iqdvXbg4JOHLpILBUSdM7XZEVDDcfuZEbI2ASaYguUGAroSh97GMngcSeFFFerMdI+/dyGy1o+GW
Ax5FxfAbFwoviajuc+DCIwn+RTwGRmRIThXxdQJyu+z4/NUDYz2DKCsILuERWsoQfoQhqpLhyhMZ
XfcknBmU0NLvQArpTm0SsI5mqKqKuFoGc8cUcjrtqLohom1AgtujQnapmJJU+BbwCLIwhJXyiKlh
MB4TkFgvIK3JjrRmAefJm+77Eiqvi+SvCq/qJahQyWuVuEpcIa7QLh7Kbsourb9b66/pZdAd1voz
fCNfwsp46OnZQPojSX9UFcNy+mYJNDeJPHtJfqeR/nSaPTzmwlXar5dQ1adpd+B//I9/hi0xuCPQ
Nkvb5um37Wtc+auQXZsVxEVYD5hnCilxTaYYjsuxLlsxXUitzd2hs3GWHLM5UOM7Fy8t3xiat4fb
sneNxmNb/POO1pRXc7vnF2nc13Rq0cFWiyXkuHmzxuOtzUYfC7fEmK/3mx4QZd5u4E7XJWz6+dey
Za4tXHUiPyB8Vm781oaT+3fN6Y/eUFDfPkcNWetNxb+tlxEZsPqPdZMOzS4rxwJ8CDC+ABj1+Tu0
d+N0hqezcjblboJ3Bj8ARJilHX4FAAA=
EOF
);

// Insert data using bind parameters
$sql = "INSERT INTO foo VALUES (?)";
$stmt = $conn->prepare($sql);
$stmt->bindParam(1, $icon, PDO::PARAM_LOB, null, PDO::SQLSRV_ENCODING_BINARY);
$stmt->execute();

// Query, fetch
$sql = "SELECT icon from foo";
$stmt = $conn->query($sql);

echo $stmt->fetchColumn();

Output

1F8B0808944009580203646F637472696E652E69636F00ED54DB4B147114FE26B58BD7D5CD55B1BC94AB666A174BAA5DD332BB42174D734DF396BA69A6AD695AA2421444599141440F26D8050A89841EA250EAA187EC21825E2A905E8C7C687EB3CECEEC65663BA3A505F51F74866FCE7C677E9CF39D6F60008E2E9D0E745F08AB1F6000904CA012E230539F0E0E73C1CD71AFD78B04AB806BC3328A2F8B18FBE4864F3E8FA1D72EB4DE7120FBEC1426BE2BD09731F4BF70E2F93B17AC37459C7B2821A345805D52F07142417435436AA3808A3E11AF3EB8D17957426031435E8F1D4EB78AA76F5DB83824E1CBA482C151274CED76445430DC7EE6446C8D8049A620B94180AE84A1F7B18C9E07127851457AB31D23EFDDC86CB5A3E196031E45C5F01B170A2F89A8EE73E0C22309FE453C064664484E15F1750272BBECF8FCD503633D83282B082EE1115ACA107E8421AA92E1CA13195DF7249C1994D0D2EF400AE94E6D12B08E66A8AA8AB85A0673C714723AEDA8BA21A26D4082DBA34276A9989254F816F008B2308495F288A961301E1390582F20ADC98EB46601E7C99BEEFB122AAF8BE4AF0AAFEA25A850C96B95B84A5C21AED02E1ECA6ECA2EADBF5BEBAFE965D01DD6FA337C235FC2CA78E8E9D940FA23497F5415C372FA66093437893C7B497EA791FE749A3D3CE6C255DAAF9750D5A76977E07FFC8F7F862D31B823D0364BDBE6E9B7ED6B5CF9AB905D9B15C445580F98670A29714DA6188ECBB12E5B315D48ADCDDDA1B371961CB33950E33B172F2DDF189AB787DBB2778DC6635BFCF38ED6945773BBE71769DCD7746AD1C1568B25E4B879B3C6E3ADCD461F0BB7C498AFF79B1E1065DE6EE04ED7256CFAF9D7B265AE2D5C75223F207C566EFCD68693FB77CDE98FDE5050DF3E470D59EB4DC5BFAD971119B0FA8F75930ECD2E2BC7027C0830BE0018F5F93BB477E37486A7B37236E56E8277063F004498A51D7E050000

I wonder if that is expected behaviour because it does work properly with other PDO drivers and also sqlsrv.

@Hadis-Knj
Copy link

@deeky666 Could you send the sqlsrv script that reproduces this issue?

@Hadis-Knj
Copy link

Hadis-Knj commented Feb 3, 2017

You should set the PDO::SQLSRV_ENCODING_BINARY for fetch statement to retrieve the decoded binary data, so if you modify the code as below you'll get the data as inserted

 // Query, fetch
 $sql = "SELECT icon from foo";
 $stmt = $conn->query($sql);
 $stmt->setAttribute(constant('PDO::SQLSRV_ATTR_ENCODING'), PDO::SQLSRV_ENCODING_BINARY);
 echo base64_encode($stmt->fetchColumn());

@deeky666
Copy link
Author

deeky666 commented Feb 3, 2017

@Hadis-Fard here is my script for sqlsrv:

$connectionInfo = array(
    'UID' => 'sa',
    'PWD' => 'D0ctrine',
);

$conn = sqlsrv_connect('mssql, 1433', $connectionInfo);

$query = "DROP TABLE IF EXISTS foo";
sqlsrv_query($conn, $query);

$query = "CREATE TABLE foo (icon VARBINARY(MAX))";
sqlsrv_query($conn, $query);

$icon = base64_decode(<<<EOF
H4sICJRACVgCA2RvY3RyaW5lLmljbwDtVNtLFHEU/ia1i9fVzVWxvJSrZmoXS6pd0zK7QhdNc03z
lrpppq1pWqJCFERZkUFEDybYBQqJhB6iUOqhh+whgl4qkF6MfGh+s87O7GVmO6OlBfUfdIZvznxn
fpzznW9gAI4unQ50XwirH2AAkEygEuIwU58ODnPBzXGv14sEq4BrwzKKL4sY++SGTz6PodcutN5x
IPvsFCa+K9CXMfS/cOL5OxesN0Wceygho0WAXVLwcUJBdDVDaqOAij4Rrz640XlXQmAxQ16PHU63
iqdvXbg4JOHLpILBUSdM7XZEVDDcfuZEbI2ASaYguUGAroSh97GMngcSeFFFerMdI+/dyGy1o+GW
Ax5FxfAbFwoviajuc+DCIwn+RTwGRmRIThXxdQJyu+z4/NUDYz2DKCsILuERWsoQfoQhqpLhyhMZ
XfcknBmU0NLvQArpTm0SsI5mqKqKuFoGc8cUcjrtqLohom1AgtujQnapmJJU+BbwCLIwhJXyiKlh
MB4TkFgvIK3JjrRmAefJm+77Eiqvi+SvCq/qJahQyWuVuEpcIa7QLh7Kbsourb9b66/pZdAd1voz
fCNfwsp46OnZQPojSX9UFcNy+mYJNDeJPHtJfqeR/nSaPTzmwlXar5dQ1adpd+B//I9/hi0xuCPQ
Nkvb5um37Wtc+auQXZsVxEVYD5hnCilxTaYYjsuxLlsxXUitzd2hs3GWHLM5UOM7Fy8t3xiat4fb
sneNxmNb/POO1pRXc7vnF2nc13Rq0cFWiyXkuHmzxuOtzUYfC7fEmK/3mx4QZd5u4E7XJWz6+dey
Za4tXHUiPyB8Vm781oaT+3fN6Y/eUFDfPkcNWetNxb+tlxEZsPqPdZMOzS4rxwJ8CDC+ABj1+Tu0
d+N0hqezcjblboJ3Bj8ARJilHX4FAAA=
EOF
);

$query = "INSERT INTO foo VALUES (?)";
$params = array(
    array(
        &$icon,
        SQLSRV_PARAM_IN,
        SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_BINARY),
        SQLSRV_SQLTYPE_VARBINARY('max')
    )
);
$stmt = sqlsrv_prepare($conn, $query, $params);
sqlsrv_execute($stmt);

$query = "SELECT icon from foo";
$stmt = sqlsrv_query($conn, $query);
$result = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_NUMERIC);

var_dump($result[0] === $icon);

I do not understand why I have to specify PDO::SQLSRV_ENCODING_BINARY on the statement when the driver is inspecting the column type on fetch anyways and should be able to infer the correct encoding and type. Using sqlsrv proofs that it should be possible. Also specifying PDO::SQLSRV_ENCODING_BINARY at statement level is not an option because not all of the columns potentially have the same enconding while fetching.

@Hadis-Knj Hadis-Knj added this to Backlog in msphpsql Feb 3, 2017
@Hadis-Knj Hadis-Knj moved this from Backlog to Staged in msphpsql Feb 3, 2017
@Hadis-Knj Hadis-Knj self-assigned this Feb 10, 2017
@Hadis-Knj Hadis-Knj moved this from Staged to In Progress in msphpsql Feb 10, 2017
@Hadis-Knj Hadis-Knj added the bug label Feb 11, 2017
@Hadis-Knj Hadis-Knj moved this from In Progress to Completed in msphpsql Feb 22, 2017
yitam added a commit that referenced this issue Feb 22, 2017
ulvii pushed a commit that referenced this issue Mar 14, 2017
* fixed Github #182, empty bound output parameters

* Fixing the issue with buffered result sets, when connection option is set to utf8. See issue #192

* removing new lines

* Replcaing SQLSRV_ENCODING ( CP_UTF8) with SQLSRV_ENCODING_UTF8

* Replcaing SQLSRV_ENCODING ( CP_UTF8) with SQLSRV_ENCODING_UTF8

* fixed tab and spacing

* updated the code structure

* deleted pdo_sqlsrv obsolete folder

* Updated pdo driver source code path

* added pdo shared folder

* updated sqlsrv and pdo_sqlsrv config.w32 to get the dir name dynamically

* deleted shared folder inside pdo

* Initial AppVeyor CI test build.

* Remove AppVeyor whitelist for testing.

* Fix source file path.

* Update pdo_util.cpp

* changed tab to spaces

* fixed indentations

* fixed indentations

* moved templates to driver folder

* Enable build matrix for different SQL Server versions.

* fixed the limit for row count

* fix #173, #138 on Windows code

* fixed indentations

* revert core_results to 365220a

* Readme and Change updates for Windows release.

* Fix table rendering.

* Readme and Change updates for Windows release.

* Update change log.

* Readme and Change updates for Windows release.

* Remove Linux section.

* merged Linux to CPP files

* merged Linux to php_sqlsrv.h

* Merged Linux and Windows PDO_SQLSRV code

* merged core_conn from linux to Windows

* modified based on reviews

* updated to version 4.1.5

* fixed indentations

* updated the date to Jan 19

* updated core_init

* Small ifdef fix

* Spacing fixes

* Commented code removed

* fixed preprocessor directives

* merged core_stmt from Linux

* merged conversion functions in core_sqlsrv and core_util

* config.m4

* Capitalised win32

* Commented code removed

* merged core_sqlsrv.h

* merged core_stream

* merged core_results

* added Linux specific files

* added Linux specific files

* removed windows header

* removed header files included in core_sqlv from xplat

* Issue 228. Don't allow 0 for sqlsrv ClientBufferMaxKBSize.

Add new tests for sqlsrv and pdo.

* Issue 37. sqlsrv_has_rows does not fetch if already true.

Change sqlsrv_has_rows to not scroll if already true
when using forward only cursor.

* gaurded strsafe header

* Update pdo_dbh.cpp

* Update pdo_stmt.cpp

* Update pdo_init.cpp

* Update pdo_parser.cpp

* Remove comment line from test.

* Update test comments.

* Add Description section to phpt test.

* fixed included headers order

* fixed indentations and spacing

* fixed indentations on core_results

* fixed indentations on rowCount

* fixed indentations in updated code

* fixed indentations

* fixed indentations

* bumped version to windows version

* made the ifdef consitent

* fixed L166 indentations

* bumped version to 4.1

* bumped up the version to 4.1

* bumped up the version to 4.1

* updated to version 4.1

* updated version to 4.1

* Added config.m4

* Added build directory

* fixed closing } dropped indentations

* fixed core_conn

* renamed header files with the same name as windows headers

* renamed <intsafe> in xplat.h to xplat_intsafe

* safeguard get_col_meta

* edited included headers

* fixed invalid user pass issue in core_conn

* added TravisCI to merged-code branch

* updated renamed header

* removed comments

* updated travis docker with latest from linux branch

* applied peer review comments

* added packagize.sh

* fixed get_processor_arch comparisons

* removed unused sqlsrv_zend_hash_add

* Update srv_075_database_wide_string.phpt

* Update srv_075_database.phpt

* Update srv_074_database.phpt

* Update srv_074_database_wide_string.phpt

* Update srv_073_database.phpt

* Merge remaining PHP-7.0-Linux changes into merged codebase.

* Change $sample calculation to avoid conversion to float.

* Change FORMAT SQL to CAST as SQL 2008 does not support FORMAT.

* modified ifdef __linux__

* Update srv_037_transaction_rollback.phpt

* Tab/indentation fixes.

* replace __linux__ with _WIN32 define.

* Update Readme and Changelog for merged code.

* Add re2c and gcc to readme steps.

* Update version.h

* removed inconsistent code calling convert_string_from_utf16

* corrected comment

* Change branch links to dev in README

* Test: Bind values with PDO::PARAM_BOOL

* Update pdo_023.phpt

* Update README.md

Add unixODBC 2.3.1 limitation for connection pooling with PDO.

* define SQL_GUID for PECL

* check if constant defined first

* In closeCursor check if statement has been executed before moving to next result.

* Change tab to spaces.

* Fix for issue 223

* added a test

* Modified the test 

to make it more robust

* Fix error message in close cursor. Change prepare to query in test.

* Fix and tests for issue #69

* fix formatting

* Fix issue #35

* add a simple test case with an input file

* simplified the test without image input

* Fix clang compile errors.

* Update README.md

* Add check for SQL_NO_TOTAL for SQLBindParameter out parameter.

* Update PHPT comment.

* Update PHPT comment and script.

* calculate field size instead of using column size and fixed the parameter encoding

* Remove uchar.h usage from buffered results.

* fix PHP7.1 debug abort error

* Add buffered sqlsrv test.

* use static cast for SQLSRV_ENCODING

* Update phpt comment.

* fixed issue #270

* test fetch binary

* added autonomous setup to test

* caps description

* Modify string_to_number utf8 conversion.

* Added survey

* fixed indentation and typo

* fixed indentations

* Changed db to tempdb and random table name

* fixed string quotes and free stmt

* fixed comment

* Change errno check to ERROR_SUCCESS

* Fix baseline for #297 changes.

* batch 1

* batch 2

* Fix memory leaks in buffered resultsets.

* batch 3

* modified tests based on review

* Added SELinux note for RedHat

* Issue #308

* Fix spacing

* Use a simpler way

* String release first

* String release first

* Modified based on review

* Modified note

* added a sqlsrv test, a variant

* Update srv_308_empty_output_param.phpt

* Update README.md

* appveyor.yml now outputs details of failing tests

* updated versioning to semantic versioning #282

* Update README.md

* changed locale for mac

* added client info tests

* added versioning notes

* updated precedence order example

* added 4.1.7 to changedlog

* updated PECL package version

* Update CHANGELOG.md

* Update CHANGELOG.md

* Update CHANGELOG.md

* part 1 with utf8 data

* MAC OS X announcement

* part 2 testing diff API

* part 3 testing rowCount

* part 4 fetching various types

* part 5 new sqlsrv tests

* removed redundant tests

* fixed some tests

* modified a failing test

* one more test

* Mac instructions (#319)

* Initial Mac instructions

* Add pecl install for mac

* Changing instructions for loading the drivers

* Updating apache install intructions for MAC

* Adding Mac related notes

* Minor cleanup

* appveyor.yml now outputs details of failing tests

* updated versioning to semantic versioning #282

* Update README.md

* changed locale for mac

* added client info tests

* added versioning notes

* updated precedence order example

* added 4.1.7 to changedlog

* updated PECL package version

* Update CHANGELOG.md

* Update CHANGELOG.md

* Update CHANGELOG.md

* MAC OS X announcement

* Updating Mac Apache Instructions. Also removing the step of manually adding drivers to the ini file.

* Adding php7.1-xml installation

* Apache config for Mac

* Fixing Apache config

* Update PECL instructions

* Fixed headers formatting
@ulvii
Copy link
Contributor

ulvii commented Apr 17, 2017

Hi @deeky666 ,

Could you confirm if this issue has been resolved with the latest release? https://github.com/Microsoft/msphpsql/releases/tag/v4.1.8-preview
Thanks

@morozov
Copy link
Contributor

morozov commented Dec 26, 2017

@ulvii the issue is not reproducible on v4.3.0. Thank you.

bburnichon pushed a commit to bburnichon/dbal that referenced this issue Oct 8, 2018
…es/270

Enabled testFetchLongBlob() for PDO SQL Server driver
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
msphpsql
Completed
Development

No branches or pull requests

5 participants