Store pictures with MySQL and its display

xiaoxiao2021-03-06  46

During our design and production of websites, sometimes the picture is saved to the database more convenient than the form of files. PHP and MySQL This can easily implement the above functions for gold combinations. In this article, we will introduce the reader how to save the picture to the MySQL database and how to display the pictures in the database.

Setting the database We usually differ from the fields of the text or integer type used in the database and the fields that need to save the image, the amount of data required for both needs to be saved. The MySQL database uses a special field to save large capacity data, and the data type is blob.

The mysql database is defined for BLOB as follows: The BLOB data type is a large binary object that saves variable number of data. Blob has four types, which are Tinyblob, Blob, MediumBlob, and longblob, which distinguishes that the maximum data length that can be saved is different.

After describing the data type you need, we can use the following statement to create a data sheet for saving the image. Create Table Images (Picnum Int Not Null Auto_Increment Primary Key, Image Blob);

Write up the upload on how to implement the file, we will not introduce it here. Now, we mainly look at how to receive upload files and store them into the MySQL database. The specific script code is as follows, where we assume that the name of the file is on the name of Picture.

This way, we can successfully save the picture to the database. If you have a problem during the process of inserting a picture into MySQL, you can check the size of the maximum packet allowed by the MySQL database. If the set value is too small, we will find the corresponding record in the database's error log.

Below, we will briefly explain the above script. First, we pass "if ($ Picture! =" None ") check if there is file being uploaded. Then, use the addslashes () function to avoid data format errors. Finally, connect MySQL, select the database and insert the picture.

Shows the picture After knowing how to deposit the image into the database, we need to consider how to take a picture from the database and display it in the HTML page. This process should be slightly complex, let's introduce the implementation process.

Because the PHP display image needs to send the corresponding header, we will face such a problem, that is, only one picture can only be displayed because we cannot send other headers after the header is issued.

In order to effectively solve this problem, we have written two files. Among them, the first file is a template for the HTML page, and the location of the image is positioned. The second file is used to actually output the file stream from the database, as the SRC attribute of the tag. The simple form of the first file can be as follows:

Picnum / ">";}?>

When the HTML page is browsed, a second.php3 file will be called every time the picture is displayed. When the second file is called, the corresponding Picture ID is incremented, and we can take it back from the database and display it.

The second.php3 file is as follows:

image;?>

At this point, we will introduce the whole process of saving and displaying images using php and mysql. The article is all simpler, and the reader can add other functions according to their actual needs, so that the entire program is more complete.

转载请注明原文地址:https://www.9cbs.com/read-54679.html

New Post(0)