php - Insert image into sql server db with pdo -
$stmt=$con->query("insert tbl(data) values(0x".$data1['hex'].")");
this sql statement , works fine. value
0xffd8ffe000104a46494600010101006000600000ffdb00430...
gets stored on database , have checked, image gets stored. trying using pdo , stored value different , not show image. here code
$datastring = file_get_contents("image.jpg"); $data1 = unpack("h*hex", $datastring); $data = '0x'.$data1['hex']; $stmt=$conp->prepare("insert tbl(data) values(:data)"); $stmt->bindparam(':data', $data); $stmt->execute();
the value in database
0x30786666643866666530303031303461343634393436303...
what making difference? doing wrong it? using sql server 2008r2 microsoft pdo_odbc driver on php 5.6.
first google hit mssql varbinary pdo
: https://social.msdn.microsoft.com/forums/sqlserver/en-us/221dcea2-438d-4a3a-b438-b98bff8f3d57/using-pdostatementbindvalue-to-update-varbinary-fields
$sth->bindparam(1, $pswd, pdo::param_lob, 0, pdo::sqlsrv_encoding_binary);
therefore, $stmt->bindparam(':data', $data, pdo::param_lob, 0, pdo::sqlsrv_encoding_binary);
should work?
edit: aaaaah, stupid me: of course doesn't work. you're not passing binary, string arbitrary ascii string happens hex encoded. go site this: http://www.rapidtables.com/convert/number/hex-to-ascii.htm, paste 0x30786666643866666530303031303461343634393436303
, convert ascii, , get? 0xffd8ffe000104a4649460
, original data.
what happens pdo/mssql thinks you're passing binary data converts hex, 0
ascii 30
hex, x
78
, f
66
, on, hope idea.
the difference first, working example subtle: don't put quotes around value passed (0x...
), hence, it's treated "true binary" in form of hex. in pdo approach, value passed is, so-to-say, internally "quoted" pdo, e.g. prevent sql injection attacks. if put quotes around first example, should same results pdo.
what's do? forget hex encoding , let odbc driver / mssql handle conversions. pass $datastring
instead of $data
, should fine.
Comments
Post a Comment