Save and output images in PHP and MySQL _php tutorial

Source: Internet
Author: User
Tags php and mysql php3 file
In the process of designing and making websites, it is sometimes easier to save images to a database than to save them in a file format. and MySQL this gold combination can easily achieve the above functions. In this article, we'll show you how to save images to a MySQL database and how to display them in a database.

Setting up the database
The difference between the number of text or integer types that we typically use in a database and the fields that need to be used to save the picture is that the amount of data that they need to save is different. The MySQL database uses specialized fields to hold large volumes of data, with a blob of data type.

The MySQL database defines the blob as follows: The BLOB data type is a large binary object that can hold a variable amount of data. BLOBs have four types, Tinyblob,blob, Mediumblob, and Longblob, respectively, and 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 data table that holds the image.
CREATE TABLE Images (picnum int not NULL auto_increment PRIMARY KEY, Image BLOB);

Writing an upload script edu-cn.com
about how to implement file upload, we will not introduce here, interested readers can see the "Web Pottery Bar" in the relevant articles. Now, let's take a look at how to receive the uploaded file and deposit it into the MySQL database. The specific script 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 ("Can ' t Perform query");
} else {
echo "You do not upload no picture";
}
?>

In this way, we can successfully save the image to the database. If you're having trouble inserting a picture into MySQL, check the size of the largest packet 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.

Let's briefly explain the above script. First, we check if there are files being uploaded through "if ($Picture! =" None "). Then, use the addslashes () function to avoid data formatting errors. Finally, connect to MySQL, select the database and insert the picture.

www.edu4u.com.cn

Show pictures
After knowing how to put the picture into the database, we need to consider how we can take the picture out of the database and display it in the HTML page. This process is a little more complicated, so let's introduce the implementation process.

Because displaying a picture requires sending a corresponding header, we face the problem of displaying only one picture at a time because we cannot send another header after the header is emitted.

In order to solve this problem effectively, we have prepared two documents. Where the first file is the template for the HTML page, 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:



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\" > ";
}
?>


www.edu4u.com.cn

When an HTML page is browsed, a second.php3 file is called every time a picture is displayed. When the second file is called, the corresponding picture ID is passed in, and 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;
?>


Excerpted from nine days galaxy

http://www.bkjia.com/PHPjc/478453.html www.bkjia.com true http://www.bkjia.com/PHPjc/478453.html techarticle In the process of designing and making websites, it is sometimes easier to save images to a database than to save them in a file format. and MySQL this gold combination can easily achieve the above work ...

  • Related Article

    Contact Us

    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.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.