read BLOB from mysql using php PDO

六眼飞鱼酱① 提交于 2019-12-24 02:55:07

问题


I have a database with a BLOB field (weeklyOccupancy). I am trying to access the data in PHP using:

$sqlCmd = 'select weeklyOccupancy from Occupancy order by startDate;';
$pdoStmt = $dbh->query($sqlCmd);
$pdoStmt->bindColumn(1, $lob, PDO::PARAM_LOB);
$pdoStmt->fetch(PDO::FETCH_BOUND);
foreach($pdoStmt as $row){
    $weeklyData = stream_get_contents($lob); 
    ....
}

However, stream_get_contents says that $lob is a string (named "Resource id #1) although I believe it should be a stream. I have seen this mentioned as a bug (http://www.php.net/manual/en/pdo.lobs.php#96311) but the workaround is not relevant for my application - in which the blob holds a bit string not an image to be displayed.

Any ideas how I can get the data out of a blob field in PHP? Thanks


回答1:


Not all PDO drivers return a LOB as a file stream; mysql 5 is one example. You can try treating lob as a string after the bind.




回答2:


Oops. There was an earlier error in my code. Problem gone.



来源:https://stackoverflow.com/questions/8920007/read-blob-from-mysql-using-php-pdo

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!