During our website design and production process, it is more convenient to save images to the database than to save them as files. And MySQL, which can easily implement the above functions. In this article, we will show you how to save images to the MySQL database and how to display images in the database.
Set Database
The difference between the text or integer fields used in the database and the fields used to save images is that the data volume to be saved is different. The MySQL database uses special fields to store large data volumes. The data type is blob.
MySQL database defines blob as follows: BLOB data type is a large binary object that can save a variable amount of data. Blob has four types: tinyblob, blob, mediumblob, and longblob. The difference is that the maximum data length that can be stored is different.
After introducing the required data types, we can use the following statement to create a data table for storing images.
Create Table images (picnum int not null auto_increment primary key, image BLOB );
Write upload script edu-cn.com
We will not introduce how to upload files here. For interested readers, refer to the related section in "Web taoba ".Article . Now, let's take a look at how to receive the uploaded files and store them in the MySQL database. Specific script Code In the following example, we assume that the name of the file upload domain is picture.
<?
If ($ picture! = "NONE "){
$ Psize = filesize ($ picture );
$ Mysqlpicture = addslashes (fread (fopen ($ picture, "R"), $ psize ));
Mysql_connect ($ host, $ username, $ password) or die ("unable to connect to SQL Server ");
@ Mysql_select_db ($ dB) or die ("unable to select database ");
Mysql_query ("insert into images (image) values '($ mysqlpicture')") or die ("can't perform query ");
} Else {
Echo "You did not upload any picture ";
}
?>
In this way, we can successfully Save the image to the database. If an error occurs when inserting an image into MySQL, check the maximum data packet size allowed by the MySQL database. If the value is too small, we will find the corresponding records in the Database Error Log.
Below, we will briefly describe the above script Program . First, we use "if ($ picture! = "NONE") "to check whether a file has been uploaded. Then, use the addslashes () function to avoid data format errors. Connect to MySQL, select a database, and insert an image.
Www.edu4u.com.cn
Show Image
After learning how to store images to the database, we need to consider how to retrieve images from the database and display them on the HTML page. This process is a little more complex. Next we will introduce the implementation process.
Because the corresponding header needs to be sent to display the image, we will face the problem that only one image can be displayed at a time, because we cannot send other headers after the header is sent.
To effectively solve this problem, we have written two files. The first file is used as the HTML page template to locate the position where the image is displayed. The second file is used to output the file stream from the database as the src attribute of the label.
The simple format of the first file can be as follows:
<HTML>
<Body>
<?
Mysql_connect ($ host, $ username, $ password) or die ("unable to connect to SQL Server ");
@ Mysql_select_db ($ dB) or die ("unable to select database ");
$ Result = mysql_query ("select * from images") or die ("can't perform query ");
While ($ ROW = mysql_fetch_object ($ result )){
Echo " picnum \ "> ";
}
?>
</Body>
</Html>
Www.edu4u.com.cn
When an HTML page is browsed, the second. php3 file is called every time an image is displayed. When the second file is called, the corresponding picture ID is passed in. We can retrieve the corresponding image from the database and display it.
The second. php3 file is as follows:
<?
$ Result = mysql_query ("select * from images where picnum = $ picnum") or die ("can't perform query ");
$ ROW = mysql_fetch_object ($ result );
Header ("Content-Type: image/GIF ");
Echo $ row-> image;
?>