Home >Backend Development >PHP Tutorial >How to operate BLOB fields in MySQL using PHP

How to operate BLOB fields in MySQL using PHP

墨辰丷
墨辰丷Original
2018-05-18 09:16:281775browse

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 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 (&#39;$COMPRESS_CONTENT&#39;)");//数据插入到数据库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 (&#39;$COMPRESS_CONTENT&#39;)");//数据插入到数据库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"];
?>

##Related recommendations:

How to remove blob pictures from mysql

javascript - How to parse the video address starting with blob in the video tag

java - Mysql blob type reading problem!

The above is the detailed content of How to operate BLOB fields in MySQL using PHP. For more information, please follow other related articles on the PHP Chinese website!

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