Home  >  Article  >  Backend Development  >  PHP操作Mysql中的BLOB字段

PHP操作Mysql中的BLOB字段

WBOY
WBOYOriginal
2016-06-23 13:49:13801browse

1、MySQL中BLOB字段类型

BLOB类型的字段用于存储二进制数据。
MySQL中,BLOB是个类型系列,包括:TinyBlob、Blob、MediumBlob、LongBlob,这几个类型之间的唯一区别是在存储文件的最大大小上不同。
MySQL的四种BLOB类型

TinyBlob:  最大 255字节
Blob:      最大 65K
MediumBlob:最大 16M
LongBlob:  最大 4G

注意:如果你存储的文件过大,数据库的性能会下降很多。

2、PHP操作BLOB案例

       [1]操作新闻内容

<?php mysql_connect( "localhost", "root", "password"); //连接数据库 	mysql_select_db( "database"); //选定数据库 	//数据插入:	$CONTENT="测试内容";   //$CONTENT为新闻内容	$COMPRESS_CONTENT = bin2hex(gzcompress($CONTENT));		$result=mysql_query( "insert into news (content) value ('$COMPRESS_CONTENT')");//数据插入到数据库news表中		//展示:	$query = "select data from testtable where filename=$filename"; 	$result = mysql_query($query);	$COMPRESS_CONTENT=@gzuncompress($result["COMPRESS_CONTENT"]);	echo $COMPRESS_CONTENT;?>
       [2]存储图片

<?php mysql_connect( "localhost", "root", "password"); //连接数据库 mysql_select_db( "database"); //选定数据库    //存储:$filename="" //这里填入图片路径 $COMPRESS_CONTENT = addslashes(fread(fopen($filename, "r"), filesize($filename)));//打开文件并规范化数据存入变量$data中$result=mysql_query( "insert into news (content) value ('$COMPRESS_CONTENT')");//数据插入到数据库test表中//展示:ob_end_clean();Header( "Content-type: image/gif");$query = "select data from testtable where filename=$filename"; $result = mysql_query($query);echo $result["COMPRESS_CONTENT"];?>

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn