大对象(LOB)

大对象(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$typePDO::PARAM_STR256);
$stmt->bindColumn(2$lobPDO::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$fpPDO::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$fpPDO::PARAM_LOB);

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

51CTO下载中心


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值