SANTOSH VERMA SANTOSH VERMA - 4 months ago 9
MySQL Question

Fetch images from database of the same column name

I am trying to write one query in which the images will be fetched in a table column from database of the same column name.
Here is my code:

<?php
$connect=new mysqli("localhost","root","") or die(mysqli_connect_error());
mysqli_select_db($connect,'go-web') or die(mysqli_connect_error());
$query=mysqli_query($connect,"SELECT * from product where name like 'Groc%'");
while($row=mysqli_fetch_array($query)){
$sql="SELECT image from product where name like 'Groc%'";
$query1=mysqli_query($connect,$sql);
$dff=mysqli_fetch_row($query1);
$image=$dff[0];
echo '<td style="border: 1px solid black;height:100px"><img src="'.$image.'" width="150px" height="150px" /><center><figcaption>Price:</figcaption></center><br> <center><figcaption>Stock:</figcaption></center></td>';
}
?>


But in this script I am getting the same image again and again.
Any help
Thank you in advance.

Answer

1) * fetch the all columns from table so don't need second select query.

try this

        <?php
    $connect=new mysqli("localhost","root","") or die(mysqli_connect_error());
    mysqli_select_db($connect,'go-web') or die(mysqli_connect_error());
    $query=mysqli_query($connect,"SELECT * from product where name like 'Groc%'");
    while($row=mysqli_fetch_array($query))
    {

    // $sql="SELECT image from product where name like 'Groc%'";
    // $query1=mysqli_query($connect,$sql);
    // $dff=mysqli_fetch_row($query1);
    // $image=$dff[0];

    echo '<td style="border: 1px solid black;height:100px"><img src="'.$row['image'].'" width="150px" height="150px" /><center><figcaption>Price:</figcaption></center><br> <center><figcaption>Stock:</figcaption></center></td>';

    }
    ?>