MySQL Settings database
The text or integer type of fields that we usually use in the database and the fields that need to save the picture are different in the amount of data they need to keep. The MySQL database uses specialized fields to hold large volumes of data, and the data type is a blob.
The MySQL database defines a blob as follows: A BLOB data type is a large binary object that can hold variable amounts of data. BLOBs have four types, respectively, Tinyblob,blob, Mediumblob, and Longblob, which differ in the maximum length of data they can hold.
After describing the type of data you need to use, we can use the following statement to create a datasheet that holds the image.
CREATE TABLE Images (picnum int not NULL auto_increment PRIMARY KEY, Image BLOB);
Write upload Script
about how to implement file upload, we are no longer introduced here, interested readers can see the "Web Tao Bar" in the relevant articles. Now, let's take a look at how to receive uploaded files and deposit them in the MySQL database. The specific scripting code is as follows, where we assume that the file upload domain name 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 ("Cant perform query");
}else {
echo "You did upload no picture";
}
?>
In this way, we can successfully save the picture to the database. If you are having problems inserting a picture into MySQL, check the size of the maximum packets allowed by the MySQL database. If the setting value is too small, we will find the corresponding record in the error log of the database.
Below, let's briefly explain the scripting procedures described above. First, we check if any files are uploaded by "if" ($Picture!= "None"). Then, use the addslashes () function to avoid data formatting errors. Finally, connect MySQL, select the database and insert the picture.
Show pictures
After knowing how to put the diagram? After entering the database, we need to consider how we can take pictures out of the database and display them in the HTML page. This process is a bit more complicated, let's introduce the implementation process.
Because the PHP display image needs to send the corresponding headers, so we will face a problem, that is only one picture at a time, because we can not send the headers after the other headers.
In order to solve this problem effectively, we have prepared two documents. Where the first file is used as the template for the HTML page, locate the location where the picture is displayed. The second file is used to actually output the file stream from the database as the SRC attribute of the label.
The simple form 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 ("Cant perform query");
while ($row =mysql_fetch_object ($result)) {
echo "picnum\ ">";
}?>
</BODY> </HTML>
When an HTML page is browsed, a second.php3 file is invoked every time a picture is displayed. When the second file is invoked, the corresponding picture ID is passed in so that we can retrieve the corresponding image from the database and display it.
SECOND.PHP3 files are as follows:
?
$result =mysql_query ("SELECT * from Images WHERE picnum= $PicNum") or Die ("Cant perform query");
$row =mysql_fetch_object ($result);
Header ("Content-type:image/gif");
Echo $row->image;
?>
So we've covered the whole process of saving and displaying pictures using PHP and MySQL. All of these are the simplest examples, the reader can add some other functions according to their actual needs to make the whole program more perfect.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.