How to Insert Images into a MySQL Database Using PHP

php


In this tutorial, we will show how you can insert images into a MySQL table using PHP.

Below is an example of a MySQL table which contains images:

Fruits

Banana
$0.79
Mango
$1.99
Cantaloupe
$3.99
Honeydew
$4.99

The above table shown is a table from a MySQL database. You can see the images to the left of the table. This is not HTML. This is PHP code. So now you know that I know how to build a MySQL table and insert images.

How to Insert Images into a MySQL Database Table

In order to insert images into a MySQL database, we don't actually put the images in the database. We put the images in our regular folder of our website, which most people do by placing images in their images folder. In the MySQL database, all you do is place the image name along with the image type using the format image_name.image_type into the table. For example, we may name a picture of a clown, clown.jpg. This is the only piece of information we place in our MySQL table.

We will go through this below.

In this demonstration, I'm going to use phpMyAdmin. When creating a table in MySQL, you must create a separate column for storing and then showing images. I name this column images, which I will use to later store all the paths to the images and then display them. So when you create your table, create a separate images column.

Creating an images column in mysql

To create an images column, all we must do is put it to type Varchar and put a good enough length. Above we chose 50.

Now to insert an image into the table, all we put do is type the image name and image format using the format image_name.image.format. Thus, if we have a image file named clown and it's a jpg file, we place clown.jpg into the table. This is shown below:

Images

Once we place this into the table, along with the other column information upon insertion of all the row data, we are finishing with the MySQL part of this program.

We now can proceed to the PHP code to extract the data from the MySQL database table and display, such as the table shown above.

PHP Code

The PHP Code of the table shown above is:

The first block of PHP code creates php variables which store the username, password, host name, database name, and table name of our MySQL server. We will need this later to connected to the database we want to connect to.

The second block of PHP code connects to the MySQL database. If you're unfamiliar with this, check out the resource How to Connect to a MySQL database using PHP.

The third block of PHP code selects the columns which we want to show in our table. The only reason we use this is because we do not want to show our primary ID column. And, specifically in this table, we use the ORDER By column to place the elements in order by price. You may not want to do this. This is just an extra feature. If not, just delete the "ORDER By Price" line, and it will not order the rows using any method.

The fourth block of code creates a table that has a length of 540px. You can create any length size which you want. Of course, if you make it too small, it won't be able to hold the images and text so there is a minimum size but you can expand it as you want. The while loop fetches each of the rows of data in the table. We can a $images_field PHP variable which represents the value in the column of images for each row. We then create a next PHP variable which contains the path of the image we will show in the database. Because I stored all of the images to be shown in the database in the images folder, the path of all the images are in /images/. We then retrieve all the other values in the other two columns, which are named Type and Price. All of the other elements in the program are to structure and show table and show the text and image of each row.

And this is how images can be inserted into MySQL tables of databases.


Related Resources

How to Insert Videos Into a MySQL Database Using PHP

How to Select a Table from a MySQL Database

How to Create a Confirmation Page for an HTML Web Form Using PHP

How to Redirect to Another URL with PHP

How to Create a Search Engine Using PHP

How to Upload Images to a Website Using PHP

How to Upload Files to a Website Using PHP

How to Create a Searchable Database Using MySQL and PHP

How to Search a MySQL Table For Any Word or Phrase Using PHP

How to Create Your Own File Transfer Protocol (FTP) For Your Website Using PHP

How to Create a Register and Login Page Using PHP



HTML Comment Box is loading comments...