I have manually stored my image path in phpmyadmin database and wish to retrieve the image by calling the path on the webpage. The image appears when I type path: http://glenc.sg-host.com/image/bca-project.jpg onto the address bar. However, all I get is the actual text of the file path as opposed to the image itself. if it’s a datatype issue, What would be the appropiate one to use?
<?php
$conn = mysqli_connect('localhost', 'unmgdk38mwzfa', 'rootpasswor6', 'dbdee5szj7qh3e');
//check connection
if(!$conn){
echo 'connection error: ' . mysqli_connect_error();
}
//check get request id parameter
if(isset($_GET['id']))
{
$id = mysqli_real_escape_string($conn, $_GET['id']);
//make sql
$sql = "SELECT * FROM latest_news WHERE news_id = $id";
//get query result
$result = mysqli_query($conn, $sql);
//fetch result in array format
$pizza = mysqli_fetch_assoc($result);
//free result from memory
mysqli_free_result($result);
//close connnection
mysqli_close($conn);
}
?>
<!DOCTYPE html>
<html>
<head>
<title><?php echo ($pizza['wtitle'])?></title>
</head>
<body>
<section class="more-feature-area">
<div class="container main">
<div class="row">
<div class="header col-sm-12 text-center">
<h1 class="text-center" data-aos="fade-zoom-in"><?php echo ($pizza['news_title']); ?></h1>
</div>
<div class="col-sm-12">
<div class="feature-list">
<ul>
<li>
<div class="feature-details">
<p><?php echo nl2br($pizza['news_text']); ?></p>
</div>
</li>
</ul>
</div>
</div>
<div class="col-sm-12"><?php echo ($pizza['image']); ?></div>
</div>
</div>
</section>
</body>
</html>
2
Answers
Use img tag like
There is only path stored in your
$pizza['image']
variable.So if you do
The result is
Browser interprets this html as text. If you want to display image you have to put the url of img as src attribute of
<img>
tag. You can do it like thisIt’s also a good practice to use the
alt
attribute ofimg
element to provide alternative text describing the image. This is used when the image cannot be reached or when the device displaying the page cannot display images (for example screen readers). I’ve used thenews_title
as alt in my example but you can use whatever you find appropriate.