使用 php PDO 从 mysql 读取 BLOB
我有一个带有 BLOB 字段(weeklyOccupancy)的数据库。我正在尝试使用 PHP 访问数据:
$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);
....
}
然而,stream_get_contents 说 $lob 是一个字符串(名为“Resource id #1”),尽管我相信它应该是一个流。我已经看到这被提到为一个错误(http:// /www.php.net/manual/en/pdo.lobs.php#96311)但解决方法与我的应用程序无关 - 其中 blob 包含一个位字符串而不是要显示的图像
任何想法 。数据PHP 中的 blob 字段?
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
并非所有 PDO 驱动程序都将 LOB 作为文件流返回; mysql 5 就是一个例子。您可以尝试在绑定后将
lob
视为字符串。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.哎呀。我的代码中有一个较早的错误。问题消失了。
Oops. There was an earlier error in my code. Problem gone.