Safwan Faidhi Safwan Faidhi - 2 months ago 11
MySQL Question

how to display image from database in one page php html?

I just wrote some code that should display an output of all product in database. In my code its only display one item, not all the items in the database. I think it is the problem at "view1.php?id= " , its did not seem to work at all.

<?php
mysql_connect("localhost", "root", "") OR DIE (mysql_error());
mysql_select_db ("global") OR DIE ("Unable to select db".mysql_error());

$sql = "SELECT * FROM images";
$res = mysql_query($sql);
$row = mysql_fetch_array($res);

$id=$row['id'];
$name=$row['name'];
$image=$row['image'];
$price=$row['price'];
?>
<form action="view1.php" method="GET">
<a href="view1.php?id= <?php echo $id ?>">
<img style="width: 250px; height=250px; float: left;" src="data:image/jpeg;base64,<?php echo base64_encode($row['image'])?>"/>
<br><strong><?php echo $name ?></strong>
<p style="font-size: 30px;">RM : <strong><?php echo $price ?><strong></p>
<p><input type="submit" name="submit" value="Add to Cart"></p>

</form>

Answer

Try This one... If not work end the '}' after the form inside a php tag like this <?php } ?>

<?php
mysql_connect("localhost", "root", "") OR DIE (mysql_error());
mysql_select_db ("global") OR DIE ("Unable to select db".mysql_error());


 $res =mysql_query( "SELECT * FROM images");
 while($row = mysql_fetch_assoc($res)){

 $id=$row['id'];
 $name=$row['name'];
 $image=$row['image'];
 $price=$row['price'];
 }
?>
<form action="view1.php" method="GET">
<a href="view1.php?id= <?php echo $id ?>">
<img style="width: 250px; height=250px; float: left;" 
src="data:image/jpeg;base64,<?php echo base64_encode($row['image'])?>"/>
<br><strong><?php echo $name ?></strong>
<p style="font-size: 30px;">RM : <strong><?php echo $price ?><strong></p>
<p><input type="submit" name="submit" value="Add to Cart"></p>

</form>