简体   繁体   中英

how to display image from database using php and mysql in a html page

This is my php and mysql retrive code

<?php
    include_once("config.php");
    $image_id = $_GET['image_id'];

    $res = mysqli_query($mysqli,"SELECT * FROM image WHERE image_id = $image_id");
    while($row = mysqli_fetch_assoc($res))
    {
    $image=$row['path_txt'];
    //echo $image;
    $path ="<img src='http://localhost:8080/memes/".$image."' />";
    echo $path;

    }
   ?>

This is my HTML code:

<html> <head> </head> <body> <img src=urlencode("sample.php?image_id=58")/> <img src="<?php include("sample.php?image_id=58");?>"/> <?php echo "<img src= 'localhost:8080/memes/sample.php?image_id=58'/>";; echo "<a href =urlencode("/sample.php?image_id=58")>"; ?> </body> </html>

It's better if you create a function for get image source and then use. Like below.

functions.php

<?php
  function getImgSrc ($image_id) { 
     $res = mysqli_query($mysqli,"SELECT * FROM image WHERE image_id = $image_id LIMIT 0, 1");
     $row = mysqli_fetch_assoc($res); 
     $image=$row['path_txt'];
     //echo $image;
     $path ="<img src='http://localhost:8080/memes/".$image."' />";
     return $path;

}

In index.php where you need to display that image.

<?php 
  // include functions.php
  include 'functions.php';  
?>
 <img src="<?php echo getImgSrc(58); ?>"/>

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