网站首页 美食营养 游戏数码 手工爱好 生活家居 健康养生 运动户外 职场理财 情感交际 母婴教育 时尚美容

怎么使用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));

© 2025 小知经验
信息来自网络 所有数据仅供参考
有疑问请联系站长 site.kefu@gmail.com