php中如何从数据库中读取图片?

php中如何从数据库中读取图片?,第1张

<?php

//将图片存进数据库再读出,注意存储图片的字段类型必须为blob

$user=’root’

$password=’root’

$db=’test’

$connect=mysql_connect(‘localhost’,$user,$password)

mysql_set_charset(‘utf8′,$connect)

mysql_select_db($db)

$photo = “0x”.bin2hex(file_get_contents(“./test.jpg”))

$sql=”INSERT INTO `test`.`test` (`photo`) VALUES ($photo)”//$photo不需要用引号,切记

mysql_query($sql)

//$result=mysql_query(“SELECT *

//FROM `test`

//LIMIT 0 , 30〃)

//$img=mysql_fetch_array($result)

//echo $img['photo']

?>

给你提供个ACCESS版的VB代码,使用时调用这些过程即可:

'使用ADODB.Stream来保存/读取图像文件到数据库

'引用Microsoft ActiveX Data Objects 2.5 Library及以上版本

'保存文件到数据库中

Sub SaveFile()

Dim Stm As New ADODB.Stream

Dim Cnn As New ADODB.Connection

Dim rs As New ADODB.Recordset

Dim strCnn As String

strCnn = "Provider=Microsoft.Jet.OLEDB.4.0Persist Security Info=FalseData Source=" &_

App.Path &"\DB1.mdb"

Cnn.Open strCnn

'读取文件到内存(二进制模式)

With Stm

.Type = adTypeBinary

.Open

.LoadFromFile App.Path + "\Image1.bmp"

End With

With rs

.Open "SELECT * FROM TABLE1", Cnn, 1, 3

.AddNew

.Fields("IMAGE") = Stm.Read

.Update

End With

rs.Close

Stm.Close

Set rs = Nothing

Set Cnn = Nothing

Set Stm = Nothing

End Sub

'从数据库中读取图像文件

Sub ReadFile()

Dim Stm As New ADODB.Stream

Dim Cnn As New ADODB.Connection

Dim rs As New ADODB.Recordset

Dim strCnn As String

strCnn = "Provider=Microsoft.Jet.OLEDB.4.0Persist Security Info=FalseData Source=" &_

App.Path &"\DB1.mdb"

Cnn.Open strCnn

rs.Open "SELECT IMAGE FROM TABLE1 WHERE ID = 18", Cnn, adOpenKeyset, adLockReadOnly

'保存到文件

With Stm

.Mode = adModeReadWrite

.Type = adTypeBinary

.Open

.Write rs("IMAGE")

.SaveToFile App.Path + "\Image2.bmp"

End With

'显示图片

Picture1.Picture = LoadPicture(App.Path + "\Image2.bmp")

rs.Close

Stm.Close

Set rs = Nothing

Set Cnn = Nothing

Set Stm = Nothing

End Sub


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/6685709.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-03-26
下一篇2023-03-26

发表评论

登录后才能评论

评论列表(0条)

    保存