SANTOSH VERMA SANTOSH VERMA - 4 months ago 7
PHP Question

Fetching images in loop in table of same column name

Hi there I am trying to execute one code in which the images will be fetched in TABLE columns depending on the number of rows contained in the database of same column. Here i have used column name as a name to fetch the images.
Since there can be many columns of the same type so I have used loop to fetch the images from database of the same column.
Here is my code

<?php
$connect=new mysqli("localhost","root","");
mysqli_select_db($connect,'go-web');
$query=mysqli_query($connect,"SELECT * from product WHERE name LIKE 'Groc%' ");
$result=mysqli_num_rows($query);
while($result!=0){
echo '
<td style="height:200px;width:20%;">
<img src="<?php
$query1=mysqli_query($connect,"SELECT image from product where name like "Groc%"");
$result1=mysqli_query($connect,$query1);
$row=mysqli_fetch_assoc($result1);
echo $row["image"];
?>" width="150px" height="150px" /><center><figcaption>Price:</figcaption></center><br>
<center><figcaption>Stock:</figcaption></center></td>';
$result--;
}
?>


Any help?

Answer

This should work

 <?php
    $connect=new mysqli("localhost","root","");
    mysqli_select_db($connect,'database')
    $query=mysqli_query($connect,"SELECT * FROM table_name where name='$name'");
    echo "<table>";
    while($row=mysqli_fetch_array($query)){
    echo '<tr><td style="height=100px"><a href=""><img src="'.$row['image'].'" width="150px" height="150px" /></a></td></tr>';
    }
    echo "</table>";

    ?>
Comments