php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #40913 PDO_MYSQL: PDO::PARAM_LOB does not bind to a stream for fetching a BLOB
Submitted: 2007-03-25 12:29 UTC Modified: 2020-12-22 15:08 UTC
Votes:200
Avg. Score:4.7 ± 0.7
Reproduced:185 of 185 (100.0%)
Same Version:22 (11.9%)
Same OS:30 (16.2%)
From: dennis at inmarket dot lviv dot ua Assigned: nikic (profile)
Status: Closed Package: PDO MySQL
PHP Version: 7.0.13 OS:
Private report: No CVE-ID: None
 [2007-03-25 12:29 UTC] dennis at inmarket dot lviv dot ua
Description:
------------
PDOStatement::bindColumn($idx, $var, PDO::PARAM_LOB) for SELECT query makes $var a string with the BLOB Data rather than a stream, for both MySQL and SQLite.

Reproduce code:
---------------
$id = (int)$_REQUEST['book'];
$stmt = $conn->prepare("SELECT coverMime, coverImage FROM books WHERE id=$id");
$stmt->execute();
$stmt->bindColumn(1, $mime);
$stmt->bindColumn(2, $image, PDO::PARAM_LOB);
$stmt->fetch(PDO::FETCH_BOUND);
var_dump($image);

Expected result:
----------------
Resource #1

Actual result:
--------------
String(792) {GIF89a...}

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-06-19 19:06 UTC] hans at velum dot net
I believe this is probably the same issue, but I also notice that using MySQL to simply execute a SQL query which contains BLOB result columns is also returning strings instead of streams.  The difference being that I'm not using bindColumn() and specifying PDO::PARAM_LOB.  I wouldn't expect that I'd need to, since simply performing a standard fetch() works as expected w/ Postgres (and, I believe, Oracle).
 [2008-06-01 12:56 UTC] mail at matya dot hu
I am using PHP 5.2.6 with MySQL 5.0.51b, and the problem is still present.

Reproduce code:
---------------

$sql = "SELECT FileName, ContentType, Size, FileData FROM `" . File::getTableName() . "` WHERE ID=?";
$stmt = $this->_DB->prepare($sql);
$stmt->execute(array($FileID));
$stmt->bindColumn(1, $name, PDO::PARAM_STR, 255);
$stmt->bindColumn(2, $type, PDO::PARAM_STR, 255);
$stmt->bindColumn(3, $size, PDO::PARAM_INT);
$stmt->bindColumn(4, $data, PDO::PARAM_LOB);

Expected result:
----------------
 [2008-06-01 13:00 UTC] mail at matya dot hu
I am using PHP 5.2.6 with MySQL 5.0.51b, and the problem is still
present.

Reproduce code:
---------------

$sql = "SELECT FileData FROM UserFile WHERE ID=?";

$stmt = $DB->prepare($sql);
$stmt->execute(array($FileID));
$stmt->bindColumn(1, $data, PDO::PARAM_LOB);
$stmt->fetch(PDO::FETCH_BOUND);
echo gettype($data);

Expected result:
----------------
resource

Actual result:
--------------
string
 [2008-08-11 17:06 UTC] mattgrdinic at yahoo dot com
Can verify this with PDO->mssql using 5.2.6 and Windows Server 2003. 

// Reproduction code:
$VIN = $_GET['VIN'];
	$imageUrl = $_GET['ImageUrl'];
	$sql_img = "SELECT VIN, Image, ImageUrl FROM ImagesDemo WHERE VIN = :VIN AND ImageUrl =  :imageUrl";
	$result_img = Database::pdo_prepare_query($sql_img, array(':VIN' => $VIN, ':imageUrl' => $imageUrl));
	$result_img->bindColumn('Image', $lob, PDO::PARAM_LOB);
	$result_img->fetch(PDO::FETCH_BOUND);
	header("Content-type: image/pjpeg");
        var_dump($lob); // string(4096) + raw image data
	fpassthru($lob); // this call fails with 'supplied argument is not a valid stream resource'
	echo $lob; // this call only spits out 4096 bytes, chopping the image off

End result -- unable to use pdo for blobs.
 [2008-09-30 16:27 UTC] ralfbecker at egroupware dot org
I can reproduce this bug with php5.2.6 on OpenSUSE 11.0

Ralf
 [2009-04-25 14:44 UTC] jani@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5.2-latest.tar.gz
 
For Windows:

  http://windows.php.net/snapshots/


 [2009-05-03 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 [2009-05-29 14:25 UTC] linuxuser586 at yahoo dot com
Problem still exists on Windows 2003 with PHP Version 5.2.10RC2-dev Build Date May 29 2009 11:39:28
 [2009-06-30 14:13 UTC] ralfbecker at egroupware dot org
Hi,
I still can reproduce the Problem with php5.3.0RC2.
Ralf
 [2009-07-27 15:12 UTC] ralph dot reckert at web dot de
That error still does not seem to be solved in the current PHP version 5.3.0 nor in 5.2.10. At least it is not listed in the ChangeLog list of fixed bugs of that versions. 

It would be nice if, four years after PDO was released and almost two and a half years after that bug was first reported, someone could try to fix it, really test that fix and make shure it finds it's way into some release version of PHP. 

Until then, in my opinion the status of this bug report should be OPEN!

Best regards,
Ralph
 [2010-02-19 21:02 UTC] jcook at macmillanmarketing dot com
I've just been able to reproduce this bug today running PHP 5.3.1 on 
Windows.

Reproduce Code:

$db = db::getConn();
$st = $db->prepare($sql);
$st->execute(array(':id' => $id));
$st->bindColumn(1, $image, PDO::PARAM_LOB);
$st->fetch(PDO::FETCH_BOUND);
header ('Content-type: image/jpeg');
fpassthru($image);// this call fails with 'supplied argument is not a
valid stream resource'

Expected result:
Resource

Actual result:
string

I can work around this error for what I'm trying to do but it would be 
nice if this was fixed.
 [2010-08-24 11:22 UTC] david at futuresbright dot com
I have experienced the same issue with Windows 2003 using SQL Server 9.0 and php 5.2.13.

Really disappointing that this bug can have been around for so many years without being fixed.
 [2010-10-05 17:14 UTC] samuel at ingessons dot se
Is there a way to solve the problem and get a stream?
I want to make a pipeline directly from a longblob to the client.
 [2010-10-05 17:23 UTC] samuel at ingessons dot se
The reason why I want to have a stream and not echo it directly is that I use a function that should return a stream.
 [2010-10-05 18:10 UTC] samuel at ingessons dot se
And another reason why I need a stream and not a string is that a string would consume all the memory for a large object.
 [2010-11-06 01:50 UTC] felipe@php.net
-Assigned To: wez +Assigned To: mysql
 [2010-11-22 10:43 UTC] uw@php.net
-Status: Verified +Status: Feedback
 [2010-11-22 10:43 UTC] uw@php.net
5.2 only gets security fixes, if at all. 

Can this be reproduced with 5.3.4-RC, using libmysql (recent one!), using mysqlnd?
 [2010-12-27 01:02 UTC] php at paloch dot net
Seems to be fixed in 5.3.4 final. I was struggling with this issue (and PHP 5.2.16) all evening before finding this post. I tried installing PHP 5.3.4 and voila, problem solved.
 [2011-01-04 13:21 UTC] uw@php.net
-Status: Feedback +Status: Closed
 [2011-01-04 13:21 UTC] uw@php.net
See last comment, works with 5.3.4
 [2011-10-20 09:48 UTC] oridan82 at gmail dot com
PHP 5.3.5
PARAM_LOB is returning a string instead of a stream.

Code:
-----
$stmt = $conn->prepare($qry);
$stmt->bindValue(1, $_GET['id']);
$stmt->bindValue(2, $_GET['aid']);
$stmt->execute();

$stmt->bindColumn(4, $mimeType, PDO::PARAM_STR, 256);
$stmt->bindColumn(6, $lob, PDO::PARAM_LOB);
$stmt->fetch(PDO::FETCH_BOUND);

echo gettype($lob);


Expected Result:
----------------
resource


Actual Result:
--------------
string
 [2012-08-04 12:40 UTC] david dot palella at gmail dot com
The problem is still present with PHP v. 5.3.10 and Ubuntu 12.10
 [2012-11-07 08:55 UTC] vicrry at yahoo dot com dot hk
As of PHP 5.3.15, this bug still exists.
 [2012-11-07 12:15 UTC] johannes@php.net
The implementation for this is broken in PDO core. For PGSQL there is a workaround documented:

    Since information about the columns is not always
    available to PDO until the statement is executed,
    portable applications should call this function after 
    PDOStatement::execute().

    However, to be able to bind a LOB column as a stream
    when using the PgSQL driver, applications should call
    this method before calling PDOStatement::execute(),
    otherwise the large object OID will be returned as an integer.
    http://www.php.net/manual/en/pdostatement.bindcolumn.php

This works neither works with sqlite nor mysql drivers. After research I assume it won't work with other drivers either. The LOB-handling happens in the param_hook for PDO_PARAM_EVT_EXEC_PRE. But this event only happens if there are bound parameters during execute.
But result columns are bound after execute ...

The support for this feature has do be redesigned in PDO core ... as MySQL has no native stream support I don't support adding a hack for this.
 [2012-11-07 12:15 UTC] johannes@php.net
-Status: Closed +Status: Re-Opened
 [2012-11-07 12:19 UTC] johannes@php.net
-Assigned To: mysql +Assigned To:
 [2013-05-31 05:25 UTC] ssufficool@php.net
PGSQL binds to a stream object for a LOB. Which PDO driver is this referring to?
 [2013-06-12 04:33 UTC] ssufficool@php.net
-Summary: PDO::PARAM_LOB does not bind to a stream for fetching a BLOB +Summary: PDO_MYSQL: PDO::PARAM_LOB does not bind to a stream for fetching a BLOB
 [2014-01-01 12:53 UTC] felipe@php.net
-Package: PDO related +Package: PDO MySQL
 [2014-01-24 09:52 UTC] heruan at aldu dot net
Still present in PHP 5.4.9
 [2014-03-19 13:13 UTC] romanoaugusto88 at gmail dot com
Still in PHP 5.5.9
 [2014-03-27 22:16 UTC] benjamin dot morel at strictcoding dot co dot uk
This must be the oldest unsolved PHP bug: 7 years!
Still broken with PHP 5.5.10.

Could a lead developer comment on this please?
 [2014-10-09 09:33 UTC] b27296 at trbvm dot com
Bug is still present in 5.5.17
 [2014-12-03 18:08 UTC] brian at idltg dot in
This is still an issue with PHP 5.5.9 with MS SQL Server 2008 R2. Result is a string instead of a stream.
 [2015-05-09 22:49 UTC] markus at malkusch dot de
Well, it's 2015 now and still present in PHP-5.6.5.
 [2016-03-08 11:36 UTC] joshua at paddle dot com
It's now 2016, and I'm using PHP 5.6.18, and this issue is STILL present. I wonder if it's been fixed in php 7...
 [2016-03-08 12:39 UTC] benjamin dot morel at gmail dot com
Still the same with PHP 7.0.4 and pdo_mysql. It returns a string instead of a resource.

PHP 7 would have been THE opportunity to fix weird behaviours such as this one. Missed again.

It's been 9 years now, since this bug has been open. Maybe we can wait for another 5 years before PHP 8 is released, and this bug can be fixed. Or not.

The fear to break backwards compatibility is SO enormous within the PHP community, that everyone prefers to carry on with bugs, rather than fixing them, so that bad code can continue working from one version to the other. Even major releases.
 [2017-02-03 08:44 UTC] yunosh@php.net
-Operating System: Win XP +Operating System: -PHP Version: 5.2.6 +PHP Version: 7.0.13
 [2018-04-11 13:54 UTC] samuel dot chemla at orange dot com
Hi,
it seems this bug won't be fixed soon.
Could you please at least update the doc to reflect that it is broken?
 [2018-05-28 15:21 UTC] benjamin dot morel at gmail dot com
Wow, 11 years.

> The fear to break backwards compatibility is SO enormous within the PHP community, that everyone prefers to carry on with bugs, rather than fixing them, so that bad code can continue working from one version to the other.

That is so true, and one of the reasons why PHP is still being poorly considered by so many engineers.
 [2018-10-24 14:20 UTC] php at bohwaz dot net
Yup, I can confirm. Mentioned it in https://wiki.php.net/rfc/implement_sqlite_openblob_in_pdo

Will look into fixing that issue if I get some free time.
 [2020-12-18 16:23 UTC] nikic@php.net
-Status: Re-Opened +Status: Assigned -Assigned To: +Assigned To: nikic
 [2020-12-22 15:08 UTC] nikic@php.net
-Status: Assigned +Status: Closed
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Oct 13 17:01:27 2024 UTC