怎么使用PDO来操作bytea

 时间:2024-11-09 12:49:00

1、新建一个数据库的表,叫做blobstorecreate table blobstore( id serial primary key, doc varchar, blob bytea);

2、创建一个php文件,输入下面的文本<?php $dsn = 'pgsql:dbname=test;host=127.0.0.1'; $user = 'user'; $password = 'password'; // these variables must be set to match the actual connection try { $dbh = new PDO($dsn, $user, $password); } catch (PDOException $e) { die('Connection failed: ' . $e->getMessage()); } $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

3、保存到bytea的代码为:$data = bin2hex(file_get_contents($filename));try{ $sql="insert into blobstore (doc,blob) values('test',?)"; $sqlh= $dbh->prepare($sql); $sqlh->execute(array($data)); } catch(Exception $e){ die($e->getMessage()); } print("<p>Done</p>");

4、添加读取bytea的代码:$sql="sele罕铞泱殳ct blob from cddoc where id=?"; $sqh=$dbh->prepare($sql); $sqh->execute(array($_GET['id'])); $data=$sqh->fetchAll(PDO::FETCH_NUM); $data=$data[0][0]; // print($data) here will just return "Resource id # ..." header('Content-Type: image/png'); // must be adjusted accordingly for other file types, maybe filetype stored as a field in the table? $data=fgets($data); // The data are returned as a stream handle gulp all of it in in one go, again, this may need some serious rework for too large files print(pack('H*',$data));

  • pdo文件用什么打开
  • thinkphp pdo怎样使用m方法
  • php 怎么使用PDO连接mysql?
  • php中如何使用pdo的方式连接并操作数据库mysql
  • windows PDO_Mysql怎么装
  • 热门搜索
    哪里有化妆学校 旅游宣传口号 新股怎么申购简单流程 巴马长寿村在哪里 东风日产逍客怎么样 蛤蟆草泡水喝的功效 高丽参泡水喝的功效 路由器怎么插线 你在哪里英文 皮草如何清洗和保养