mysql - PHP PDO MS Access how to read blob images? -
i have images blob's in ms access database. have far used them odbc acces php , works fine. here comes 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 medlemmer id=17"; $rd = odbc_exec($con, $sql); if (odbc_fetch_row($rd)) { echo odbc_result($rd,"photo"); } odbc_close($con); ob_end_flush(); ?> i in process of converting mysql have use ms access timg: therefor making new code using pdo, not able read data correct.
here comes 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 medlemmer 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 mysql (changed connection string) not ms access.
i have searced net long time have not been able find solution.
can pleasse?
i use first code, need able handle blob's other purposes well.
php , access odbc driver have never been best of friends, , apparently continues case pdo_odbc , access odbc driver. 2 wrinkles here were
the blob returned ascii string representing hex values of image data (e.g., '424d7ac000...'), and
that string contains spurious null character every 255 characters.
the code managed working is:
<?php $dbname = $_server["document_root"]."\\test.mdb"; $con = new pdo("odbc:driver={microsoft access driver (*.mdb)}; dbq=$dbname; uid=; pwd=;"); $sql = "select photo clients id=?"; $st = $con->prepare($sql); $st->execute(array(1)); $st->bindcolumn(1, $photochars, pdo::param_lob); $st->fetch(pdo::fetch_bound); // $photochars long string of hex, e.g., '424d7a...' // pdo+access_odbc apparently injects null every 255 characters, // remove them first $photochars = str_replace("\0", "", $photochars); // create array of character pairs (e.g.: '42', '4d', '7a', ...) $photoarray = str_split($photochars, 2); // convert numeric values ($i = 0; $i < sizeof($photoarray); $i++) { $photoarray[$i] = hexdec($photoarray[$i]); } // pack binary string // ref: http://stackoverflow.com/a/5473057/2144390 $photodata = call_user_func_array("pack", array_merge(array("c*"), $photoarray)); header('content-type: ' . image_type_to_mime_type(imagetype_png)); header('content-disposition: attachment; filename="untitled.bmp"'); echo $photodata;
Comments
Post a Comment