lopez cesar lopez cesar - 2 months ago 7
MySQL Question

how to put the image from mysql to php table cell



<?php
$images = 'image';
$sql = "SELECT image FROM items WHERE itemsID = '4';";
$result = mysqli_query($connection, $sql) or die(mysqli_error($connection));
?>

<div id="table1" style="position:absolute; overflow:hidden; left:199px; top:139px; width:"241px"; height:"163px"; z-index:43">
<div class="wpmd">
<?php
while($rows = mysqli_fetch_assoc($result)) {
?>
<div><TABLE bgcolor="#FFFFFF" border=1 bordercolorlight="#C0C0C0" bordercolordark="#808080">
<TR valign=top>
<TD width=231 height=155><img src="images/<?php $rows[$images];?><BR>
</TD>
</TR>
<?php
}
?>





//this should be work but not
//i putting an image get from the the database then put it into table cell but i keep getting this error enter image description here

Answer

If you need to display the image from the mysql which is storing the image name that you have uploaded you have to follow the steps in order to display it.

  1. Ensure under which folder you have saved the uploaded image.
  2. Make sure that you store only the name of the image into the database so that we can retrieve it very easy by looping it over the query.

Note: As per the question you have to retrieve the images from the database so that you need to follow the code which i have given below.

Method 1: To retrieve the image from the DB

Your code which displays the <?php $rows[$images];?> is wrong pertaining to that you need to display the name of the DB field over to there and echo the value and then alone you will be getting the image over there.

Replace:

<TD width=231 height=155><img src="images/<?php $rows[$images];?><BR>

With:

<TD width=231 height=155><img src="images/<?php echo $rows['images'];?><BR>

provided the $row['images'] where images is the field containing the name of the image stored in the DB. or else you can change the name as per the Table field what you have. Rather than that your code look good.

Hence you entire code looks like,

<?php
 $images = 'image';
 $sql = "SELECT image FROM items WHERE itemsID = '4';";
 $result = mysqli_query($connection, $sql) or die(mysqli_error($connection));
?>
<div id="table1" style="position:absolute; overflow:hidden; left:199px; top:139px; width:"241px"; height:"163px"; z-index:43">
<div class="wpmd">
<?php 
while($rows = $result->fetch_assoc()) { 
?>
<div><TABLE bgcolor="#FFFFFF" border=1 bordercolorlight="#C0C0C0" bordercolordark="#808080">
<TR valign=top>
<TD width=231 height=155><img src="images/<?php echo $rows['images'];?><BR>
</TD>
</TR>
<?php 
}
?>

Method 2: To retrieve the image as per your Structure

You have stored the $images='image'.

Ensure that the table has the field image where it stores the image name. If it does so you can simply change the code like this .

You are missing echo in the code so that it displays the name of the image stored in the DB and it prints that image stored under int images folder with the hep of the image name you given as input to the img tag.

Replace:

<TD width=231 height=155><img src="images/<?php $rows[$images];?><BR>

with:

<TD width=231 height=155><img src="images/<?php echo $rows[$images];?><BR>

Note: But method 2 is not advisable since if you loop through the DB for getting the value you have to follow that Method-1 alone.

Comments