image is not displaying from the database it show just showing broken image
asked Sep 25, 2015 at 16:08
15
$photo is path right
then try this
echo 'Moving from the comments, this is the fix you need:
echo 'DirtyBitDirtyBit
16.3k4 gold badges30 silver badges54 bronze badges
Change second last line with this
echo '';
This will work if $photo path is correct...
answered Sep 25, 2015 at 16:20
Generally, when we upload image file in PHP, the uploaded image is stored in a directory of the server and the respective image name is stored in the database. At the time of display, the file is retrieved
from the server and the image is rendered on the web page. But, if you don’t want to consume the space of the server, the file can be stored in the database only. You can upload an image without storing the file physically on the server using the MySQL database. It’s very easy to store and retrieve images from the database using PHP and MySQL.
If you’re concerned about the server space and need free space on your server, you can insert the image file directly in the database without
uploading it to the directory of the server. This procedure helps to optimize the server space because the image file content is stored in the database rather than the server. In this tutorial, we will show you how to store image files into the MySQL database and retrieve images from the database using PHP.
Before getting started to integrate file upload with the database, take a look at the file structure.
store_retrieve_image_from_database/
├── dbConfig.php
├── index.php
├── upload.php
├── view.php
└── css/
└── style.css
Insert Image File in MySQL
MySQL has a BLOB [binary
large object] data type that can hold a large amount of binary data. The BLOB data type is perfect for storing image data in the database. In MySQL, four BLOB types are available – TINYBLOB, BLOB, MEDIUMBLOB, and LONGBLOB. The LONGBLOB data type is perfect to store the image file data.
Create Database Table
To store the file content, a table is required in the database. The following SQL creates an images
table with the LONGBLOB data type field in the MySQL database.
CREATE TABLE `images` [
`id` int[11] NOT NULL AUTO_INCREMENT,
`image` longblob NOT NULL,
`created` datetime NOT NULL DEFAULT current_timestamp[],
PRIMARY KEY [`id`]
] ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Database Configuration [dbConfig.php]
The dbConfig.php
file is used to connect and select the database. Specify the database host [$dbHost
], username [$dbUsername
], password [$dbPassword
], and name [$dbName
] as per your MySQL database credentials.
Retrieve image from
database [view.php]
In the view.php
file, we will retrieve the image content from the MySQL database and list them on the web page.
- The data, charset, and base64 parameters in the src attribute, are used to display image BLOB from MySQL database.