首页 > Other > php中PDO操作大对象方法(blob)

php中PDO操作大对象方法(blob)

参考php中的官方文档

http://php.net/manual/en/pdo.lobs.php

1.insert blob into databse :oracle

<?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();
?>

2.insert blob into database

<?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.display blob from database

<?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);
?>

This example binds the LOB into the variable named $lob and then sends it to the browser using fpassthru(). Since the LOB is represented as a stream, functions such as fgets(), fread() and stream_get_contents() can be used on it.

注意:下面是用yii框架的pdo操作blob字段。

1.插入blob数据时:

<?php

$db= Yii::app()->db;
$stmt = $db->createCommand(“select contenttype, imagedata from images where id=?”);
$stmt->execute(array($_GET[‘id’]));
//其他的地方都一样了

?>

2.读取blob数据时:

<?php

//controller is BlobController

public function actionDisplayBlob(){

$cid = trim($_REQUEST[‘id’]);

$sql = “select content_type, content_bin as bin,content_id from mms_task_content where mms_id = $cid “;

$command = Yii::app()->db->createCommand($sql);
$row = $command->queryRow();
$type = $row[‘CONTENT_TYPE’];
$lob = $row[‘BIN’];//blob字段
header(“Content-type: “.$type);
fpassthru($lob);

}

?>

php的view页中这样显示

echo ‘<img src=”index.php?r=blob/displayblob&id=’.$id.'” />’;

分类:Other
  1. 还没有评论。
  1. No trackbacks yet.

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: