skip to Main Content

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


  1. Use img tag like

    <div class="col-sm-12"><img src="<?php echo ($pizza['image']); ?>" alt=""/></div>
    
    Login or Signup to reply.
  2. There is only path stored in your $pizza['image'] variable.

    So if you do

    <div class="col-sm-12"><?php echo ($pizza['image']); ?></div>
    

    The result is

    <div class="col-sm-12">http://glenc.sg-host.com/image/bca-project.jpg</div>
    

    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 this

    <div class="col-sm-12"><img src="<?php echo $pizza['image']; ?>" alt="<?php echo $pizza['news_title']; ?>"></div>
    

    It’s also a good practice to use the alt attribute of img 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 the news_title as alt in my example but you can use whatever you find appropriate.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search