大对象(LOB)
在您的应用程序在某些时候,你可能会发现,你需要“大”数据库中的数据存储。大通常意味着“大约4KB或多个”,虽然有些数据库可以愉快地处理高达32KB的数据之前,成为“大”。在本质上可以是文本或二进制大对象。PDO让您的工作与大型数据类型使用 PDO :: PARAM_LOB
类型的代码在您的 PDOStatement :: bindParam()或 PDOStatement :: bindColumn()调用, PDO :: PARAM_LOB
告诉PDO的数据流映射,因此,你可以操纵它使用 PHP流API。
【举例】#显示的图像从数据库中
这个例子结合的LOB到变量$吊射,然后将其发送到浏览器中使用 fpassthru() 。LOB由于被表示为一个流,如fgets()中, 弗里德(功能 )和 stream_get_contents()可以使用就可以了。
<?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, $type, PDO::PARAM_STR, 256);
$stmt->bindColumn(2, $lob, PDO::PARAM_LOB);
$stmt->fetch(PDO::FETCH_BOUND);
header("Content-Type: $type");
fpassthru($lob);
?>
【举例】#2插入到数据库中的图像
本示例打开一个文件,并通过文件处理PDO将它作为一个LOB。PDO将竭尽所能得到的文件的内容到数据库尽可能以最有效的方式。
<?php
$db = new PDO('odbc:SAMPLE', 'db2inst1', 'ibmdb2');
$stmt = $db->prepare("insert into images (id, contenttype, imagedata) values (?, ?, ?)");
$id = get_new_id(); // some function to allocate a new ID
// assume that we are running as part of a file upload form
// You can find more information in the PHP documentation
$fp = fopen($_FILES['file']['tmp_name'], 'rb');
$stmt->bindParam(1, $id);
$stmt->bindParam(2, $_FILES['file']['type']);
$stmt->bindParam(3, $fp, PDO::PARAM_LOB);
$db->beginTransaction();
$stmt->execute();
$db->commit();
?>
【举例】#3到数据库中插入图像:甲骨文
从文件中插入一个吊射,甲骨文需要一个稍微不同的语法。这也是必要的,你在一个事务中执行插入,否则将致力于新插入的LOB长度为零的一部分,在执行查询时发生的隐式提交:
<?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(); // some function to allocate a new ID
// assume that we are running as part of a file upload form
// You can find more information in the PHP documentation
$fp = fopen($_FILES['file']['tmp_name'], 'rb');
$stmt->bindParam(1, $id);
$stmt->bindParam(2, $_FILES['file']['type']);
$stmt->bindParam(3, $fp, PDO::PARAM_LOB);
$stmt->beginTransaction();
$stmt->execute();
$stmt->commit();
?>