How to retrieve images from MySQL database and display in an html tag

后端 未结 5 1266
春和景丽
春和景丽 2020-11-22 02:34

I created a MySQL database with a table using phpmyadmin. I created this table with a BLOB column to hold a jpeg file.

I have issues with regards to the php variabl

相关标签:
5条回答
  • 2020-11-22 03:15

    Technically, you can too put image data in an img tag, using data URIs.

    <img src="data:image/jpeg;base64,<?php echo base64_encode( $image_data ); ?>" />
    

    There are some special circumstances where this could even be useful, although in most cases you're better off serving the image through a separate script like daiscog suggests.

    0 讨论(0)
  • 2020-11-22 03:28

    You need to retrieve and disect the information into what you need.

    while($row = mysql_fetch_array($result)) {
     echo "img src='",$row['filename'],"' width='175' height='200' />";
    }
    
    0 讨论(0)
  • 2020-11-22 03:29

    add $row = mysql_fetch_object($result); after your mysql_query();

    your html <img src="<?php echo $row->dvdimage; ?>" width="175" height="200" />

    0 讨论(0)
  • 2020-11-22 03:36

    You can't. You need to create another php script to return the image data, e.g. getImage.php. Change catalog.php to:

    <body>
    <img src="getImage.php?id=1" width="175" height="200" />
    </body>
    

    Then getImage.php is

    <?php
    
      $id = $_GET['id'];
      // do some validation here to ensure id is safe
    
      $link = mysql_connect("localhost", "root", "");
      mysql_select_db("dvddb");
      $sql = "SELECT dvdimage FROM dvd WHERE id=$id";
      $result = mysql_query("$sql");
      $row = mysql_fetch_assoc($result);
      mysql_close($link);
    
      header("Content-type: image/jpeg");
      echo $row['dvdimage'];
    ?>
    
    0 讨论(0)
  • 2020-11-22 03:37

    I have added slashes before inserting into database so on the time of fetching i removed slashes again stripslashes() and it works for me. I am sharing the code which works for me.

    How i inserted into mysql db (blob type)

    $db = mysqli_connect("localhost","root","","dName"); 
    $image = addslashes(file_get_contents($_FILES['images']['tmp_name']));
    $query = "INSERT INTO student_img (id,image) VALUES('','$image')";  
    $query = mysqli_query($db, $query);
    

    Now to access the image

    $sqlQuery = "SELECT * FROM student_img WHERE id = $stid";
    $rs = $db->query($sqlQuery);
    $result=mysqli_fetch_array($rs);
    echo '<img src="data:image/jpeg;base64,'.base64_encode( stripslashes($result['image']) ).'"/>';
    

    Hope it will help someone

    Thanks.

    0 讨论(0)
提交回复
热议问题