| Bug #46249 | pdo_pgsql always fill in NULL for empty BLOB | ||||
|---|---|---|---|---|---|
| Submitted: | 7 Oct 2008 6:00pm UTC | Modified: | 14 Oct 2008 1:53am UTC | ||
| From: | hswong3i at gmail dot com | Assigned to: | felipe | ||
| Status: | Closed | Category: | PDO related | ||
| Version: | 5.2.6 | OS: | Debian | ||
| Votes: | 1 | Avg. Score: | 5.0 ± 0.0 | Reproduced: | 1 of 1 (100.0%) |
| Same Version: | 1 (100.0%) | Same OS: | 1 (100.0%) | ||
[7 Oct 2008 6:00pm UTC] hswong3i at gmail dot com
[11 Oct 2008 12:26am UTC] felipe@php.net
I can't reproduce using 5.3CVS:
$db = new PDO('pgsql:host=localhost dbname=test', 'foo', 'bar',
array(PDO::ATTR_STRINGIFY_FETCHES => TRUE));
$stmt = $db->prepare("INSERT INTO test_one_blob (blob1) VALUES (?)");
$stmt->execute(array(''));
var_dump($db->errorinfo());
$stmt = $db->prepare("INSERT INTO test_one_blob (blob1) VALUES
(:foo)");
$x = '';
$stmt->bindParam(':foo', $x);
$stmt->execute();
var_dump($db->errorinfo());
$stmt = $db->query("INSERT INTO test_one_blob (blob1) VALUES ('')");
$stmt->execute();
teste=> select * from test_one_blob where blob1 is null;
id | blob1
----+-------
(0 registros)
Do you have tested using PDO directly, without Drupal's db_insert()?
[11 Oct 2008 6:47am UTC] hswong3i at gmail dot com
According to http://www.php.net/manual/en/pdo.lobs.php, using PDO for BLOB INSERT/UPDATE should come with: 1. bindParam/bindColumn 2. PDO::PARAM_LOB 3. stream API, e.g. fopen(), fwrite(), rewind(), etc. You may try to insert other value though you code snippet. The value should also NOT able to insert into PostgreSQL... The bug reproduction code in http://drupal.org/node/316095#comment-1047830 is using pdo_pgsql directly. The db_insert() used is just a fake clone of Drupal CVS HEAD function with simplified programming logic.
[11 Oct 2008 11:47am UTC] felipe@php.net
Oh sorry, you're right. I can reproduce it.
[11 Oct 2008 7:05pm UTC] felipe@php.net
This bug has been fixed in CVS. Snapshots of the sources are packaged every three hours; this change will be in the next snapshot. You can grab the snapshot at http://snaps.php.net/. Thank you for the report, and for helping us make PHP better.
[12 Oct 2008 4:39pm UTC] hswong3i at gmail dot com
I try for both PHP5.2 and 5.3 from http://snaps.php.net/ (they both coming with the fix). They are now able to handle NULL and empty string BLOB INSERT/UPDATE. Thanks for the work. BTW, it is now generate another issue that should belongs to http://bugs.php.net/bug.php?id=46274. Whenever fetching NULL or empty BLOB content from DB apache will crash with Segmentation fault. Maybe we still need some love for that :S
[12 Oct 2008 4:51pm UTC] felipe@php.net
Do you have tried after 13:03:31 2008 UTC? I've commited a complete fix to these issue in this time. Thanks.
[12 Oct 2008 5:44pm UTC] hswong3i at gmail dot com
@felipe: Yes I did. I give a compare with cvs.php.net log message, and check if my PHP package coming with the fix. Fix is already included, #46249 is fixed but than buggy for #46274 as mentioned :S I try both PHP in CLI and Apache2.2. BLOB INSERT/UPDATE are successful in both cases, but fetching NULL and empty string are both buggy...
[13 Oct 2008 2:31am UTC] hswong3i at gmail dot com
Bug still exists, but preform in another way... Please try the following
code snippet:
<?php
function _var_dump($msg) {
print("<code><pre>");
var_dump($msg);
print("</pre></code>");
}
function db_insert($sql, $data) {
global $active_db;
$stmt = $active_db->prepare($sql);
$blob = fopen('php://memory', 'a');
fwrite($blob, $data);
rewind($blob);
$stmt->bindParam(':blob1', $blob, PDO::PARAM_LOB);
$stmt->execute();
$id = $active_db->lastInsertId('test_one_blob_id_seq');
return $id;
}
function db_select($sql, $id) {
global $active_db;
$sql = 'SELECT * FROM "test_one_blob" WHERE "id" = :id';
$stmt = $active_db->prepare($sql);
$stmt->bindParam(':id', $id);
$stmt->execute();
return $stmt->fetch(PDO::FETCH_ASSOC);
}
$active_db = new PDO('pgsql:host=localhost dbname=DRUPAL_7', 'root',
'CHANGE', array(PDO::ATTR_STRINGIFY_FETCHES => TRUE));
// Test normal BLOB insert
$data = "This is\000a test.";
$id = db_insert('INSERT INTO "test_one_blob" ("blob1") VALUES (:blob1)',
$data);
$return = db_select('SELECT * FROM "test_one_blob" WHERE "id" = :id',
$id);
_var_dump("Test normal BLOB insert");
_var_dump($data);
_var_dump($return['blob1']);
_var_dump($data === $return['blob1']);
// Test NULL BLOB insert
$data = NULL;
$id = db_insert('INSERT INTO "test_one_blob" ("blob1") VALUES (:blob1)',
$data);
$return = db_select('SELECT * FROM "test_one_blob" WHERE "id" = :id',
$id);
_var_dump("Test NULL BLOB insert");
_var_dump($data);
_var_dump($return['blob1']);
_var_dump($data === $return['blob1']);
// Test empty BLOB insert
$data = "";
$id = db_insert('INSERT INTO "test_one_blob" ("blob1") VALUES (:blob1)',
$data);
$return = db_select('SELECT * FROM "test_one_blob" WHERE "id" = :id',
$id);
_var_dump("Test empty BLOB insert");
_var_dump($data);
_var_dump($return['blob1']);
_var_dump($data === $return['blob1']);
?>
[13 Oct 2008 2:32am UTC] hswong3i at gmail dot com
It is now result as: string(23) "Test normal BLOB insert" string(15) "This is�a test." string(15) "This is�a test." bool(true) string(21) "Test NULL BLOB insert" NULL string(0) "" bool(false) string(22) "Test empty BLOB insert" string(0) "" string(0) "" bool(true)
[13 Oct 2008 4:36pm UTC] felipe@php.net
You aren't working with real NULL, but with a "empty resource". It's
exaclty what happens to fwrite($fp, '') too.
Only using the code below you will get the NULL as expect:
$blob = null;
$stmt->bindParam(':blob1', $blob, PDO::PARAM_LOB);
Thanks.
[14 Oct 2008 1:53am UTC] hswong3i at gmail dot com
Thanks felipe!! After update the code snippet of db_insert() as below,
everything works fine:
<?php
function db_insert($sql, $data) {
global $active_db;
$stmt = $active_db->prepare($sql);
if (is_null($data)) {
$stmt->bindParam(':blob1', $data, PDO::PARAM_LOB);
}
else {
$blob = fopen('php://memory', 'a');
fwrite($blob, $data);
rewind($blob);
$stmt->bindParam(':blob1', $blob, PDO::PARAM_LOB);
}
$stmt->execute();
$id = $active_db->lastInsertId('test_one_blob_id_seq');
return $id;
}
?>
