downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

PDO> <Hatalar ve Ele Alınışları
[edit] Last updated: Fri, 23 Mar 2012

view this page in

Büyük Veri Nesneleri (LOB'lar)

Uygulamanızın bir yerinde veritabanınızda büyük müktarda veri saklamanız gerekebilir. Burada büyük veriden kasıt 4kB civarı olsa da bazı veritabanları için büyük veri 32kB'tan başlar. Büyük veriler ikil veya dizgesel olabilir. PDOStatement::bindParam() ve PDOStatement::bindColumn() çağrılarında büyük verilerlerle çalışmak için PDO::PARAM_LOB veri türünü kullanabilirsiniz. PDO::PARAM_LOB, verinin bir akım olarak eşleneceği anlamına gelir. Dolayısıyla, bu veriler üzerinde PHP Akım işlevleri ile işlem yapabilirsiniz.

Örnek 1 - Veritabanındaki bir resmin gösterilmesi

Bu örnekte bir LOB, $lob değişkeni ile ilişkilendirilmekte ve fpassthru() üzerinden tarayıcıya gönderilmektedir. LOB bir akımla temsil edildiğinden fgets(), fread() ve stream_get_contents() gibi işlevlerle üzerinde işlem yapabilirsiniz.

<?php
$db 
= new PDO('odbc:SAMPLE''db2inst1''ibmdb2');
$stmt $db->prepare("select contenttype, imagedata from images where id=?");
$stmt->execute(array($_GET['id']));
$stmt->bindColumn(1$typePDO::PARAM_STR256);
$stmt->bindColumn(2$lobPDO::PARAM_LOB);
$stmt->fetch(PDO::FETCH_BOUND);

header("Content-Type: $type");
fpassthru($lob);
?>

Örnek 2 - Bir resmin veritabanına yerleştirilmesi

Bu örnekte bir dosya açılıp, bir LOB olarak veritabanına yerleştirilmek üzere, dosya tanıtıcısı PDO'ya aktarılmaktadır. PDO, dosya içeriğini veritabanına yerleştirme işlemini en verimli şekilde yapacaktır.

<?php
$db 
= new PDO('odbc:SAMPLE''db2inst1''ibmdb2');
$stmt $db->prepare("insert into images (id, contenttype, imagedata) values (?, ?, ?)");
$id get_new_id(); // Yeni bir kimlik ayıran bir işlev

// Resmin bir dosya yükleme formundan geldiğini varsayıyoruz
// PHP belgelerinde  bu konuda ayrıntılı bilgi bulabilirsiniz

$fp fopen($_FILES['file']['tmp_name'], 'rb');

$stmt->bindParam(1$id);
$stmt->bindParam(2$_FILES['file']['type']);
$stmt->bindParam(3$fpPDO::PARAM_LOB);

$db->beginTransaction();
$stmt->execute();
$db->commit();
?>

Örnek 3 - Oracle'da bir resmi veritabanına yerleştirmek

Oracle, bir LOB'u bir dosyadan alıp veritabanına yerleştirirken farklı bir sözdizimi kullanımını gerektirir. Esas olarak, yerleştirme işlemi bir toplu hareket olarak gerçekleştirilir. Böyle yapılmazsa oluşturulan LOB, sorgu çalıştırıldığında örtük olarak sıfır uzunluklu bir veri girişinin parçası haline gelir.

<?php
$db 
= new PDO('oci:''scott''tiger');
$stmt $db->prepare("insert into images (id, contenttype, imagedata) " .
"VALUES (?, ?, EMPTY_BLOB()) RETURNING imagedata INTO ?");

$id get_new_id(); // Yeni bir kimlik ayıran bir işlev

// Resmin bir dosya yükleme formundan geldiğini varsayıyoruz
// PHP belgelerinde  bu konuda ayrıntılı bilgi bulabilirsiniz
$fp fopen($_FILES['file']['tmp_name'], 'rb');

$stmt->bindParam(1$id);
$stmt->bindParam(2$_FILES['file']['type']);
$stmt->bindParam(3$fpPDO::PARAM_LOB);

$stmt->beginTransaction();
$stmt->execute();
$stmt->commit();
?>



PDO> <Hatalar ve Ele Alınışları
[edit] Last updated: Fri, 23 Mar 2012
 
add a note add a note User Contributed Notes Büyük Veri Nesneleri (LOB'lar)
xorinox at gmx dot ch 13-Jan-2011 04:11
I find it easier to use stream_get_contens to fetch the data of a lob using the file handle.

<?php
$stmt
= $pdo->con->prepare( 'select * from filetable' );
$stmt->execute();
$res = $stmt->fetchAll( PDO::FETCH_ASSOC );

for(
$i=0; $i<count($res); $i++ ){
 
$filename = "C:/tmp/".$res[$i]['FILE_ID'].'.xml';
 
$content = stream_get_contents( $res[$i]['DATA_FILE'] );
 
file_put_contents( $filename, $content );
}
?>
Jeremy Cook 19-Feb-2010 06:31
There seems to be a bug that affects example 1 above. PDO::PARAM_LOB when used with pdo::bindColumn() is supposed to return a stream but it returns a string. Passing this string to fpassthru() then triggers an error with the message 'supplied argument is not a valid stream resource'. This has been reported in bug #40913. The work around is to do the following:

<?php
$stmt
= $db->prepare("select contenttype, imagedata from images where id=?");
$stmt->execute(array($_GET['id']));
$stmt->bindColumn(1, $type, PDO::PARAM_STR, 256);
$stmt->bindColumn(2, $lob, PDO::PARAM_LOB);
$stmt->fetch(PDO::FETCH_BOUND);

header("Content-Type: $type");
echo(
$lob);
?>

Since the browser is expecting an image after the call to header() writing the string representation of the binary output with echo() has the same affect as calling fpassthru().
http://matts.org/ 28-Aug-2009 07:30
A big gotcha exists for Oracle users.

You have to save CLOB objects using PDO::PARAM_STR, not PDO::PARAM_LOB.

But you MUST send the 4th argument, usually strlen($subject) or you get a LONG error.
diogoko at gmail dot com 01-Apr-2009 03:33
PDOStatement's methods bindParam and bindValue also work with strings, as in:

<?php
  $data
= file_get_contents($filename);
 
$stmt->bindValue(1, $data, PDO::PARAM_LOB);
 
//...
?>

This was the only way I could make it work with PostgreSQL.
knl at bitflop dot com 23-Sep-2008 06:07
I spend a lot of time trying to get this to work, but no matter what I did PDO corrupted my data.

I finally discovered that I had been using:

$pdo->exec('SET CHARACTER SET utf8');

in the TRY part of my connection script.

This off course doesn't work when you feed binary input to PDO using the parameter lob.

 
show source | credits | stats | sitemap | contact | advertising | mirror sites