Skip site navigation (1) Skip section navigation (2)

Can't get PHP PDO LOB working with PostgreSQL

From: Rico Secada <coolzone(at)it(dot)dk>
To: pgsql-php(at)postgresql(dot)org
Subject: Can't get PHP PDO LOB working with PostgreSQL
Date: 2008-09-25 19:19:38
Message-ID: (view raw, whole thread or download thread mbox)
Lists: pgsql-php

I have spent about two working days trying to get PostgreSQL working
with PDO inserting binary content and pulling it back out without

I have tested this on Debian Etch using PHP 5.2.0 and PostgreSQL(libpq)
8.1.11. I have enabled the PostgreSQL PDO driver.

I have set up a simple table to hold the content:

id serial
blob_type character varying
attachment oid

I am using a simple form to process the upload, and my PHP upload
script looks like this (modified a little from the PHP manual):

try {

$pdo = new PDO ("$pdo_database:host=$pdo_hostname;dbname=

} catch (Exception $e) { 

echo 'Caught exception: ',$e->getMessage(), "\n";


ini_set('max_execution_time', '0');
ini_set('memory_limit', '128M');

require_once ("knl_mime_type.php"); // Gets the correct mime type.
$mime_type = new knl_mime_type();
$blob_type = $mime_type->getMimeType($tmpfilename);


$attachment = fopen($_FILES['file']['tmp_name'], "rb");
$filename = $_FILES['file']['name'];

$stmt = $pdo->prepare("INSERT INTO attachments (blob_type, filename,
attachment) VALUES (:blob_type, :filename, :attachment)");

$stmt->bindParam(':blob_type', $blob_type, PDO::PARAM_STR);
$stmt->bindParam(':filename', $filename, PDO::PARAM_STR);
$stmt->bindParam(':attachment', $attachment, PDO::PARAM_LOB);



When I submit the form, I can see (using PHPPgAdmin) the binary file, in
this test case a PNG image being inserted.

From the table I see this info using PHPPgAdmin:

id	blob_type	filename	attachment
25	image/png	shot2.png	16441

I don't know how the binary data are supposed to look like since I am
migrating from MySQL, and only have that as a comparison.

If I understand the PostgreSQL manual correctly the above number
"16441" is a OID reference number to the binary data.

I don't know if the above is correct or if PostgreSQL has received the
binary data correctly, maybe someone can confirm this for me please?

Anyway, when I try to pull that data from the database (again using the
example from the PHP manual) I just get the reference number "16441"
back to the browser.

I am using the following code to retrieve the data:

$stmt = $pdo->prepare("SELECT blob_type, attachment FROM attachments
WHERE id = :id LIMIT 1"); 
$stmt->bindParam(':id', $_GET['id'], PDO::PARAM_STR); 


$results = $stmt->fetchAll();

foreach ($results as $row) {
	$blob_type 	= $row['blob_type'];
	$attachment = $row['attachment'];
header("Content-type: $blob_type");
echo $attachment;

If I use Firefox and take a look at the source code behind the page I
get served, I just see the number "16441" and nothing else.

Please notice that the above example are taken directly from the PHP
manual just modified a little. I have also tried using the examples
directly without any modifications, but the result is the same.

What am I missing or doing wrong here?

Best regards.



pgsql-php by date

Next:From: Rico SecadaDate: 2008-09-25 19:32:17
Subject: Can't get PHP PDO LOB working with PostgreSQL (WRONG CODE)
Previous:From: ljbDate: 2008-09-02 00:16:39
Subject: Re: pg_query_params and SQL injection

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group