简体   繁体   中英

Image not display using PHP and MySQL

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>

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

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
            }
      }
?>

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>

请务必检查echo $row['file']如果它返回具有正确文件扩展名的图像路径的正确字符串。

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 } ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM