Home > Article > Backend Development > Example of how php operates BLOB fields in MySQL
This article mainly introduces the method of PHP operating BLOB fields in MySQL, and analyzes the related operating skills of PHP using mysql's BLOB fields to store news content and pictures based on specific examples. Friends in need can refer to the following
The example in this article describes how PHP operates BLOB fields in MySQL. Share it with everyone for your reference, the details are as follows:
1. BLOB field type in MySQL
BLOB type fields are used to store binary data .
In MySQL, BLOB is a type series, including: TinyBlob, Blob, MediumBlob, LongBlob. The only difference between these types is the maximum size of the stored file.
MySQL’s four BLOB types
TinyBlob: Maximum 255 bytes
Blob: Maximum 65K
MediumBlob: Maximum 16M
LongBlob: Maximum 4G
Note: If the file you store is too large, the performance of the database will drop a lot.
2. PHP operation BLOB case
(1) Operation news content
<?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) Store pictures
<?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"]; ?>
The above is the detailed content of Example of how php operates BLOB fields in MySQL. For more information, please follow other related articles on the PHP Chinese website!