html – Image not display using PHP and MySQL-ThrowExceptions

Exception or error:

Below is my code for display information of an image including its file, but this is not displaying, I don’t know why.

 <?php
      include('config/dbconnect.php');  
      if(isset($_GET['id'])) 
      { 
            $id = $_GET['id'];
            $sql = mysqli_query($con, "SELECT * FROM collage WHERE id = '$id'");
            while($row = mysqli_fetch_array($sql)) {
                  $name = $row['name'];
                  $content = $row['content'];
                  $size = $row['size'];
                  $type = $row['type'];
                  $date_upload = $row['date_upload'];
                  $file = $row['file'];
            }
      }
?>
<div class='body-content'>
<div class='img-name-cont'><h3><?php echo $name; ?></h3></div>
<div class='img-detail-cont'>upload: <?php echo $date_upload; ?><br>type: <?php echo $type; ?><br>size: <?php echo $size; ?>KB </div>
<div class='img-file-cont'>
<img src="img/collage/<?php echo $row['file'] ?>" style="width:130px; height:100%"></div>
<div class='img-content-cont'><?php echo $content; ?></div>
</div>
How to solve:

First of all use inspect to check whether the image is actually pointed to by the code. that is the actual loation might not be pointed at. what is your folder structure.

” style=”width:130px; height:100%”>

your code is ok. as long as php echo $row[‘file’] returns a file

Answer:

The problem here is “Your all the variables are local to the while loop”. so you can’t access it outside of the while loop. Try this code.

 <?php
      include('config/dbconnect.php');  
      if(isset($_GET['id'])) 
      { 
            $id = $_GET['id'];
            $sql = mysqli_query($con, "SELECT * FROM collage WHERE id = '$id'");
            while($row = mysqli_fetch_array($sql)) {
        ?> 

                  <div class='body-content'>
                  <div class='img-name-cont'><h3><?php echo $row['name']; ?></h3></div>
                  <div class='img-detail-cont'>upload: <?php echo $row['date_upload']; ?><br>type: 
                  <?php echo $row['type']; ?><br>size: <?php echo $row['size']; ?>KB </div>
                  <div class='img-file-cont'>
                  <img src="img/collage/<?php echo $row['file']; ?>" style="width:130px; height:100%"></div>
                  <div class='img-content-cont'><?php echo $row['content']; ?></div>
                  </div>
        <?php
            }
      }
?>

Answer:

Be sure to check echo $row['file'] if it returns the correct string of your image path with the correct file extension .

Answer:

try this one :

first of all you have to check if your image path ok or not using this php function:

<?php $img=getimagesize($imagewithpath); if($img=="") { echo "there is no image ";} else{ ?> <img src="<?php echo $imagewithpath; ?>" style="width:130px; height:100%"> <?php } ?>

Answer:

As answered by Dulaj Sanjaya all your variables are local so you can’t use that outside of while.

Second if you want to use your code then you have taken all data of $row in different variables then used outside of file then why don’t you used same $file for image why you have used $row['file']?

Simply replace this line as

<div class='img-file-cont'>
                  <img src="img/collage/<?php echo $file; ?>" style="width:130px; height:100%"></div>

Leave a Reply

Your email address will not be published. Required fields are marked *