1. The type of BLOB field in MySQL
A field of BLOB type is used to store binary data.
In MySQL, BLOBs are a series of types, including: Tinyblob, BLOBs, Mediumblob, Longblob, the only difference between these types is the maximum size of the storage file.
MySQL four types of blobs
Tinyblob: Maximum 255 bytes
Blob: 65K Max
Mediumblob: 16M Max
Longblob: 4G Max
Note: If you store a file that is too large, the performance of the database will degrade a lot.
2. PHP Operation Blob case
[1] Operating news content
<?phpmysql_connect ("localhost", "root", "password"); Connect database mysql_select_db ("databases"); Selected database//data insertion: $CONTENT = "test content"; $CONTENT for news content $compress_content = Bin2Hex (gzcompress ($CONTENT)); $result =mysql_query ("INSERT INTO News" Value (' $COMPRESS _content ') ");//data is inserted into the database news table//show: $query =" Select data from testtable where Filename= $filename "; $result = mysql_query ($query); [Email protected] ($result ["compress_content"]; echo $COMPRESS _content;? >
[2] Storing pictures
<?php mysql_connect ("localhost", "root", "password");//Connect the database mysql_select_db ("database"); $filename = ""//fill in the picture path $COMPRESS _content = Addslashes (Fread (fopen ($filename, "R"), FileSize ($filename)));// Open the file and normalize the data into the variable $data $result=mysql_query ("INSERT INTO News (' $COMPRESS _content ')");//data is inserted into the Database test table/ /show: Ob_end_clean (); Header ("Content-type:image/gif"); $query = "Select data from testtable where Filename= $filename"; $result = mysql_query ($query); Echo $result ["compress_content"];? >
PHP working with BLOB fields in MySQL