Random Image Display In MySQLi using PHP

June 2, 2020
PHP
Random Image Display In MySQLi using PHP

In this tutorial, we will create a Random Image Display In MySQLi using PHP. This code can display a random image on the page when the user launches the application. The system uses MySQLi Select query to view a random picture. You need to add RAND() parameter in the ORDER BY statement. This a user-friendly program. Feel free to modify and use it for your system.

We will be using PHP as a scripting language and interpreter that is used primarily on any web server, including xamp, wamp, etc. It is being used to any popular websites because of the modern approach as its today.

Getting Started:

First, you have to download & install XAMPP or any local server that runs PHP scripts. Here’s the link for the 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.

random image in php

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
    $conn = mysqli_connect("localhost", "root", "", "db_random_image");
 
    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'];
        $exp = explode(".", $image_name);
        $end = end($exp);
        $name = time().".".$end;
        $path = "upload/".$name;
        $allowed_ext = array("gif", "jpg", "jpeg", "png");
        if(in_array($end, $allowed_ext)){
            if(move_uploaded_file($image_temp, $path)){
                mysqli_query($conn, "INSERT INTO `image` VALUE('', '$name', '$path')") or die(mysqli_error());	
                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';
        $query = mysqli_query($conn, "SELECT * FROM `image` ORDER BY RAND() LIMIT 4") or die(mysqli_error());
        while($fetch = mysqli_fetch_array($query)){
    ?>
        <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!

Facebook Comments

Leave a Reply

Your email address will not be published.