-2

I am trying to display a jpg image from database 'cemembers' under table 'users' column 'image'. With this logic I Created custom code but when I run my page in php only echo the file name stored in database but with no graphical view. example 'cdda_dd.jpg' I later inspect the image from the browser and i can see the problem might be coding a wrong image src... please look at my codes and help me out.

 <?php 

    //setting connection variables
    $hostname = "localhost";
    $username= "root";
    $password ="";
    $db= "cemembers";

    //connect to database
    $mysqli_db = new mysqli( $hostname, $username, $password, $db);

    /* check connection */
    if (mysqli_connect_errno()) {
       printf("Connect failed: %s\n", mysqli_connect_error());
       exit();
    }

    $sql = mysqli_query($mysqli_db, "SELECT * FROM users WHERE image  ");
    $row= mysqli_fetch_array($sql);
    $photo= $row['image'];
    echo $photo;
?>
Choxx
  • 945
  • 1
  • 24
  • 46
  • http://stackoverflow.com/questions/20556773/php-display-image-blob-from-mysql – kishan Mar 20 '15 at 12:22
  • So how exactly does the image tag look like you saw when you checked in your browser? You probably have to echo an image tag, so something like: `echo sprintf("", $photo);` – arkascha Mar 20 '15 at 12:23
  • 1
    You seem to be contradicting yourself, do you have an image stored as a blob in the database or do you have the path to the file stored? – jeroen Mar 20 '15 at 12:28
  • I have an image stored under table 'users' in row image in blob datatype – Jilfae Saved Andrews Mar 20 '15 at 12:33
  • i tried this : echo ''; but nothing happens . I also this: echo sprintf("", $photo); but nothing happens. @arkascha – Jilfae Saved Andrews Mar 20 '15 at 12:37
  • 1
    "Nothing happens" does not help. _What_ happens in my question back. What image tag do you get in the browser when inspecting? What does the network tab inside your browsers development console say to the request for the referenced image? What do you server log files say? – arkascha Mar 20 '15 at 12:40
  • it says and i can only see HTML5 img icon @arkascha – Jilfae Saved Andrews Mar 20 '15 at 12:44
  • So apparently the result of the call `base64_encode( $photo['image'] )` is empty. What is `$photo['image'] `? The image? Reading above I thought it is the _name_ of the image? But anyway, apparently it is empty. Don't you mean this instead: `base64_encode($photo)`? Apart from that: you did not reply to the other questions I asked, so we cannot say much more... – arkascha Mar 20 '15 at 13:38
  • alright.... am confused about what you are asking me. maybe am not getting you clear... but its okay I will try storing images in a Folder Directory. the Blob is too much complicated. @arkascha – Jilfae Saved Andrews Mar 20 '15 at 16:54
  • Why that? Loading an image from a database using base64 encoding is certainly possible and you will succeed. My impression currently is, that you are simply using the wrong variable. On the other hand that approach is questionable, it has disadvantages, especially for bigger images like photos: base64-encoded embedding does not allow caching of the image. That can be a huge disadvantage if the photo is shown more than once. – arkascha Mar 20 '15 at 18:16

1 Answers1

0

use this code..modified at line 18 & 21. select the picture from the column name "image" having id=1 or anything instead of select * from users..

<?php
    //setting connection variables
    $hostname = "localhost";
    $username= "root";
    $password ="";
    $db= "cemembers";
    //connect to database
    $mysqli_db = new mysqli( $hostname, $username, $password, $db);

    /* check connection */
    if (mysqli_connect_errno()) {
       printf("Connect failed: %s\n", mysqli_connect_error());
       exit();
    }

        $sql = mysqli_query($mysqli_db, "SELECT image FROM users WHERE id=1  ");
        $row= mysqli_fetch_array($sql);
        $photo= $row['image'];
        header("Content-type:image/jpeg"); // add this
        echo $photo;

?>
Choxx
  • 945
  • 1
  • 24
  • 46