This article mainly introduces the PHP operation of the Blob field in MySQL method, combined with specific examples of PHP using MySQL blob field to store news content and pictures of the relevant operation skills, the need for friends can refer to the next
Specific as follows:
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) Operation of news content
<?php mysql_connect ("localhost", "root", "password");//Connect Database mysql_select_db ("db");//Select 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); $COMPRESS _content= @gzuncompress ($result ["compress_content"]); echo $COMPRESS _content;? >
(2) Storing pictures
<?phpmysql_connect ("localhost", "root", "password"); Connect database mysql_select_db ("databases"); Select Database//store: $filename = ""//Fill in picture path $compress_content = Addslashes (Fread (fopen ($filename, "R"), FileSize ($filename)) )///Open file and normalize data into variable $data $result=mysql_query ("INSERT INTO News (' $COMPRESS _content ')");// Data 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"];? >
Related recommendations:
How to remove a blob image from MySQL
JavaScript-How to parse a video address that starts with a blob in a video tag
Java-mysql The BLOB type of the read problem!