skip to Main Content

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

At the moment i’m trying to display the image (BLOB) in an HTML tag to no success. I’m only getting the broken image icon when running the code.

Note: I know it’s not recommended to do this, but currently i have too.

I’ve already tried looking up various questions here in stackoverflow.com, quora.com, and codeofaninja.com, and tried implementing various solutions that seemed to have worked for other people, but they haven’t worked for me.

Where i’m displaying the information (test.php):

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

Code i’m trying to use to display image (getImage.php)

<?php
  $link = mysqli_connect("localhost", "root", "");
  mysqli_select_db("unirentas");
      $id = $_GET['id'];
      // do some validation here to ensure id is safe

      $sql = "SELECT imagen FROM propiedades WHERE ID_renta=$id";
      $result = mysqli_query("$sql");
      $row = mysqli_fetch_assoc($result);
      mysqli_close($link);

      header("Content-type: image/jpg");
      echo $row['imagen'];
?>

This should be displaying all the rows that are within the database along with the same image, but what it does is display all the rows with their corresponding information and the broken image.

2

Answers


  1. Your first error is that you have written a program which does lots of things and are then trying to work out why none of them are working. You should have created a Minimal, Verifiable and Complete script to test this and fix that first – the entirety of the first script you posted here should be replaced with:

    <html>
    <img src="getImage.php?id=10" />
    </html>
    

    You should also have posted the code which writes the data to the database.Neither you nor we know if the problem is with the data written to the database or is occurring when you retrieve the data.

    You should also be checking that you’re error logging/reporting is working correctly and that PHP is not already telling you what the error is.

    Other things you can try are writing the data to a file and checking if it you really do have a image file, looking at the headers returned by getimage.php to make sure they are what you expect.

    Finally, do take some time to learn a little about secure programming before you pug code like this into the internet – there are a lot of security problems here.

    Login or Signup to reply.
  2. I found the solution to my problem, i used the base64_encode($blob); function

    <?php
    // Server credentials 
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "unirentas";
    
    // Creating mysql connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    
    // Checking mysql connection
    if ($conn->connect_error) {
      die("Connection failed: " . $conn->connect_error);
    }
    
    // Writing a mysql query to retrieve data 
    $sql = "SELECT ID_renta, calle, smz, mz, lote, precio, universidad, fecha, imagen, nombre, telefono, correo FROM propiedades";
    $result = $conn->query($sql);
    
    if ($result->num_rows > 0) {
      // Show each data returned by mysql
      while($row = $result->fetch_assoc()) {
          $ID_renta = $row["ID_renta"];
          $calle = $row["calle"];
          $smz = $row["smz"];
          $mz = $row["mz"];
          $lote = $row["lote"];
          $precio = $row["precio"];
          $universidad = $row["universidad"];
          $fecha = $row["fecha"];
          $blob = $row["imagen"];
          $nombre = $row["nombre"];
          $telefono = $row["telefono"];
          $correo = $row["correo"];
    
    
    echo "
    
        <!-- USING HTML HERE : -->
    
        <p> ID : $ID_renta</p>
        <p> Calle : $calle </p>
        <p> Smz : $smz </p>
        <p> Mz : $mz </p>
        <p> Lote : $lote </p>
        <p> Precio :  $precio </p>
        <p> Universidad : $universidad </p>";
        echo '<img src="data:image/png;base64,'.base64_encode($blob).'"/>';
        echo "<p> Fecha_  $fecha  </p>
        <p> Nombre :  $nombre  </p>
        <p> Telefono :  $telefono </p>
        <p> Correo :  $correo  </p>
        <p>////////////////////////////////////////////////////////////////////////////////</p>
    
    ";
      }
    } else {
      echo "0 results";
    }
    
    // Closing mysql connection
    $conn->close();
    ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search