Upload image in MYSQL database & display it using PHP

41 0 0
                                        

How To upload BLOB Image To Mysql Database Using PHP,SQL And HTML

You ca easily upload and store image to mysql database using this code. Save image as a blob and display it using html. 

<?php

//This code shows how to save image im mysql database using php, sql and html.//The image is uploaded using php and sql.//It's a web-based application that can be accessed by using a browser.//This is for educational purposes only, Use it at your own risk.//Connect to server$servername = "localhost";$username = "root";$password = "";$conn = mysqli_connect($servername, $username, $password);if ($conn) {echo "Connected to server successfully";} else {die( "Failed To Connect to server ". mysqli_connect_error() );}$selectalreadycreateddatabase = mysqli_select_db($conn, "PhpMysqlDatabaseBlobImageUpload"); if ($selectalreadycreateddatabase) {echo "<br /> Existing database selected successfully";} else {echo "<br /> Selected Database Not Found";$createNewDb = "CREATE DATABASE IF NOT EXISTS 'PhpMysqlDatabaseBlobImageUpload'";if (mysqli_query($conn, $createNewDb)) {echo "<br />New Database Created Successfullly";$selectCreatedDatabase = mysqli_select_db($conn, "PhpMysqlDatabaseBlobImageUpload");if ($selectCreatedDatabase) {echo "<br />Created Database Selected Successfullly";// Creating new table $sqlcreatetable = "CREATE TABLE IF NOT EXISTS 'imageuploadphpmysqlblob' ('id' int(11) NOT NULL AUTO_INCREMENT,'name' varchar(100) NOT NULL,'image' longblob NOT NULL,PRIMARY KEY ('id')) ENGINE=InnoDB DEFAULT CHARSET=latin1;";if (mysqli_query($conn, $sqlcreatetable)) {echo "<br />New table Created";} else {echo "<br /> Unable to create new table.";}}} else {echo "Unable to create database";}}if (isset($_POST['submit'])) {if (getimagesize($_FILES['imagefile']['tmp_name']) == false) {echo "<br />Please Select An Image.";} else {//declare variables$image = $_FILES['imagefile']['tmp_name'];$name = $_FILES['imagefile']['name'];$image = base64_encode(file_get_contents(addslashes($image)));$sqlInsertimageintodb = "INSERT INTO 'imageuploadphpmysqlblob'('name', 'image') VALUES ('$name','$image')";if (mysqli_query($conn, $sqlInsertimageintodb)) {echo "<br />Image uploaded successfully.";} else {echo "<br />Image Failed to upload.<br />";}}} else {# code...}//Retrieve image from database and display it on html webpagefunction displayImageFromDatabase(){//use global keyword to declare conn inside a functionglobal $conn;$sqlselectimageFromDb = "SELECT * FROM 'imageuploadphpmysqlblob' ";$dataFromDb = mysqli_query($conn, $sqlselectimageFromDb);while ($row = mysqli_fetch_assoc($dataFromDb)) {echo '<img src=data:image;base64,'.$row['image'].'/>';}}//calling the function to display imagedisplayImageFromDatabase();//Finnaly close connectionif (mysqli_close($conn)) {echo "<br />Connection Closed.......";}?><!DOCTYPE html><html><head><title>How To upload BLOB Image To Mysql Database Using PHP,SQL And HTML.</title></head><body><form><input><br /><input></form></body></html>Video Tutorial: https://www.youtube.com/watch?v=ny8njrlaTJ4


Oops! This image does not follow our content guidelines. To continue publishing, please remove it or upload a different image.

Oops! This image does not follow our content guidelines. To continue publishing, please remove it or upload a different image.

Oops! This image does not follow our content guidelines. To continue publishing, please remove it or upload a different image.

Oops! This image does not follow our content guidelines. To continue publishing, please remove it or upload a different image.



You've reached the end of published parts.

⏰ Last updated: Apr 07, 2019 ⏰

Add this story to your Library to get notified about new parts!

How To upload BLOB Image To Mysql Database Using PHP,SQL And HTMLWhere stories live. Discover now