I am trying to get an image path from my database and display the image using HTML <img>
.
I am saving image's path in mysql database but i can't understand what's wrong.
HTML:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>image</title>
</head>
<body>
<img src="image_loading.php" width="100" height="100">
</body>
</html>
<br />
PHP:
<?php
$dbhost = "localhost";
$dbusername = "root";
$dbpass = "";
$dbname = "m_beg";
$conn = mysqli_connect($dbhost, $dbusername, $dbpass, $dbname) or die();
$sql = "SELECT `image` FROM `music_table` where `id`=100";
$res = mysqli_query($conn, $sql);
header("Location:image/jpg");
while ($row = mysqli_fetch_array($res)) {
echo $row["image"];
}
?>
Try like this..
while($row = mysqli_fetch_array($res))
{
$title = $row["image"];
echo "<img src=$title width='100' height='100'>";
}
尝试使用相对URL路径在路径中添加正斜杠:
<img src="/image_loading.php" width="100" height="100">
This is just all wrong. It will try load an image named 'image_loading.php'. The php returns an invalid header.
Instead use this, by including the php function with your html code, if that makes sense.
HTML
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>image</title>
</head>
<body>
<img src="<?php echo getfilename();?>" width="100" height="100">
</body>
</html>
<br />
PHP
<?php
function getfilename()
{
$dbhost = "localhost";
$dbusername = "root";
$dbpass = "";
$dbname = "m_beg";
$conn = mysqli_connect($dbhost, $dbusername, $dbpass, $dbname) or die();
$sql = "SELECT `image` FROM `music_table` where `id`=100";
$res = mysqli_query($conn, $sql);
while ($row = mysqli_fetch_array($res)) {
return $row["image"];
}
return "";//error no image found
}
?>
Try out this code..
<?php
$dbhost = "localhost";
$dbusername = "root";
$dbpass = "";
$dbname = "m_beg";
$conn = mysqli_connect($dbhost, $dbusername, $dbpass, $dbname) or die();
$sql = "SELECT `image` FROM `music_table` where `id`=100";
$res = mysqli_query($conn, $sql);
header("Location:image/jpg");
//change this from mysqli_fetch_array to mysqli_fetch_assoc
while ($row = mysqli_fetch_assoc($res)) {
$image=$row['image'];
echo "<img src='$image'>";
}
?>
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.