In this tutorial we will create a
Random Image Display In MySQLi using PHP. This code can display a random image in the page when user launch the application The code use MySQLi
Select query in order to display a random image you need to add
RAND() parameter in the ORDER BY statement. This a user-friendly program feel free to modify and use it to your system.
We will be using
PHP as a scripting language and interpreter that is used primarily on any webserver including xamp, wamp, etc. It is being use to any famous websites because of modern approach as its today.
Getting Started:
First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server
https://www.apachefriends.org/index.html.
And, this is the link for the bootstrap that i used for the layout design
https://getbootstrap.com/.
Creating Database
Open your database web server then create a database name in it
db_random_image, after that click Import then locate the database file inside the folder of the application then click ok.
Creating the database connection
Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it
conn.php.
<?php
if(!$conn){
die("Error: Failed to connect to database!");
}
?>
Creating The Interface
This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as
index.php.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
</head>
<body>
<nav class="navbar navbar-default">
<div class="container-fluid">
<a class="navbar-brand" href="https://sourcecodester.com">Sourceodester</a>
</div>
</nav>
<div class="col-md-3"></div>
<div class="col-md-6 well">
<h3 class="text-primary">PHP - Random Image Display In MySQLi</h3>
<hr style="border-top:1px dotted #ccc;"/>
<div class="col-md-4">
<form method="POST" action="upload.php" enctype="multipart/form-data">
<div class="form-group">
<label>Image:</label>
<input type="file" name="image" class="form-control" required="required"/>
</div>
<center><button class="btn btn-primary" name="upload"><span class="glyphicon glyphicon-save"></span> Upload</button></center>
</form>
</div>
<div class="col-md-8">
<?php include 'image_display.php'?>
</div>
</div>
</body>
</html>
Creating PHP Query
This code contains the php query of the application. This code will upload the image file to the database server. To do that just copy and write this block of codes inside the text editor, then save it as
upload.php.
<?php
require_once 'conn.php';
if(ISSET($_POST['upload'])){
$image_name = $_FILES['image']['name'];
$image_temp = $_FILES['image']['tmp_name'];
$path = "upload/".$name;
$allowed_ext = array("gif", "jpg", "jpeg", "png");
echo "<script>alert('Image saved!')</script>";
header("location: index.php");
}
}else{
echo "<script>alert('Image only')</script>";
}
}
?>
Creating the Main Function
This code contains the main function of the application. This code will display a random image when user launch the application. To make this just copy and write these block of codes below inside the text editor, then save it as
image_display.php
<div class="col-md-12">
<?php
require 'conn.php';
?>
<img src="<?php echo $fetch['location']?>" style="margin:10px; float:left;" height="150" width="150"/>
<?php
}
?>
</div>
There you have it we successfully created
Random Image Display In MySQLi using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!