omkar omkar - 1 month ago 6
PHP Question

I want to retrieve all images from database if image path is stored in database

enter image description herefor output please see the image

I have Three Images path stored In my Database and I want to fetch all these images.with styling
please help me
thanks in advance

I am Using this code But it shows only first image

<html>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$db="uni";
// Create connection
$conn = new mysqli($servername, $username, $password,$db);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$result = mysqli_query($conn,("SELECT * FROM upload_img"));
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC))
{
echo "<img src='".$row['img_name']."' />";
echo "<br />";
}
?>
</body>
</html>

Answer

I think the problem might have been the use of img_name rather than img_path as the source for the image but as an aside you start with an Object Orientated mysqli connection but switch to procedural style to run the query etc.

<html>
    <head>
        <title>Get images from db</title>
    </head>
    <body>
        <?php

            $servername = "localhost";
            $username = "root";
            $password = "";
            $db="uni";

            $conn = new mysqli( $servername, $username, $password, $db );

            if ( $conn->connect_error ) die( "Connection failed: " . $conn->connect_error );

            $result = $conn->query( "SELECT * FROM `upload_img`" );

            while( $rs = $result->fetch_object() ){
                echo "
                <img src='{$rs->img_path}' />
                <br />";
            }
        ?> 
    </body>
</html>
Comments