I have images as BLOB's in an MS ACCESS database. I have so far used them with odbc acces from PHP and it works fine. Here comes the simplified program:
code:
<?php
ini_set("odbc.defaultlrl", "5M");
$dbName = $_SERVER["DOCUMENT_ROOT"]."\\..\db\\teknofo.mdb";
$con = odbc_connect("Driver={Microsoft Access Driver (*.mdb)};Dbq=".$dbName,'','') or die('Ups');
ob_clean();
header('Content-Type: image/*');
$sql = "SELECT photo FROM Medlemmer WHERE Id=17";
$rd = odbc_exec($con, $sql);
if (odbc_fetch_row($rd)) { echo odbc_result($rd,"photo"); }
odbc_close($con);
ob_end_flush();
?>
I am in the process of converting to MySql but will have to use MS Access for some timg: Therefor I am making the new code using PDO, but I am not able to read the data correct.
Here comes the new
<?php
$dbName = $_SERVER["DOCUMENT_ROOT"]."\\..\db\\teknofo.mdb";
$con = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=$dbName; Uid=; Pwd=;");
$sql = "SELECT photo FROM Medlemmer WHERE id=?";
$st = $con->prepare($sql);
$st->execute(array(17));
$st->bindColumn('photo', $photo, PDO::PARAM_LOB);
$st->fetch(PDO::FETCH_BOUND);
odbc_longreadlen($st, 131072);
odbc_binmode($st,ODBC_BINMODE_CONVERT);
ob_clean();
header('Content-Type: image/*');
if ($rd = $st->fetch(PDO::FETCH_BOUND)) {
echo $rd['photo'];
ob_end_flush();
$con = null;
?>
The last code Works fin with MySql (changed connection string) but not with MS Access.
I have searced the net for a long time but have not been able to find a solution.
Can anybody help pleasse?
I could use the first code, but I need to be able to handle BLOB's for other purposes as well.
Using the solution from Gord Thompson I ran into a bug. I don't know why the endings were messed up on some binaries but this is how I fixed it. Sort of a hack to patch an unknown corruption source but it worked.
PHP and the Access ODBC driver have never been the best of friends, and apparently that continues to be the case with PDO_ODBC and the Access ODBC driver. The two wrinkles here were
The BLOB is returned as an ASCII string representing the hex values of the image data (e.g., '424D7AC000...'), and
That string contains a spurious NULL character every 255 characters.
The code I managed to get working is:
I know its an old thread, but i stumbled uppon the same problem and @gord-thompson's answer helped me. But I realized that there is a simpler and faster way to do it.
All you need to do is strip out the
\0
-Bytes and then invokepack
directly:So
str_split
,hexdec
is not needed anymore. All you have to do ispack
the HEX-Data that comes from PDO+Access into binary format.