[英]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.首先使用inspect来检查图像是否被代码实际指向。 that is the actual loation might not be pointed at.
那是可能没有指出的实际位置。 what is your folder structure.
你的文件夹结构是什么。
your code is ok.你的代码没问题。 as long as php echo $row['file'] returns a file
只要php echo $row['file']返回一个文件
The problem here is "Your all the variables are local to the while loop".这里的问题是“您的所有变量都是 while 循环的本地变量”。 so you can't access it outside of the while loop.
所以你不能在 while 循环之外访问它。 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
.正如 Dulaj Sanjaya 所回答的那样,您的所有
variables
都是local
variables
,因此您不能在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']
?其次,如果你想使用你的代码,那么你已经在不同的
variables
获取了$row
所有数据,然后在文件之外使用,那么你为什么不使用相同的$file
image
为什么你使用了$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 } ?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.