0

can somebody help me with this little problem here. I am a newbie and I am trying to display an image with a blob data type from my post database table. I do not know where am i going wrong but here is my code.

$res = $conn->query("SELECT post.postid, post.message,post.photo, post.posted_on, users.username FROM 
                          post INNER JOIN users ON post.userid = users.userid ORDER BY
                          post.posted_on DESC LIMIT 10");
   $result = "";
    while($row = mysqli_fetch_array($res)){
            20204040
            $image = $row['photo'];

            $result.= '<hr/><form method="post" enctype="multipart/form-data">
            <div  class="w3-light-grey w3-padding w3-round-xlarge w3-margin-60">'.
            '<strong> @'.$row['username'].'</strong>'.
            '</span>'.'<br/> '.
            '<span class ="ka">'.$row['message'].'</span>'.

            '<img src="image/$image"/>'.

            '<br/>'.
            '<span class="fa fa-comment w3-text-indigo"></span>'.
            '<a href="#" id="'.$row['postid'].'" class="w3-text-indigo w3-small w3-border-0 w3-light-grey"'.
            ' data-toggle="modal" data-target="#replyModal">'."Reply".'</a>'.
            '<span class ="w3-tiny">'.$row['posted_on'].'</div></form>';


    }
  • 1
    instead of `''.` use `"".` . Also try once to give full path instead of `image/` – Alive to die - Anant Nov 21 '16 at 11:04
  • You know that if you want to include HTML in with PHP content, instead of trying to jam all the HTML code into a single PHP string, you can just terminate the PHP tag `?>` then put in HTML the way you normally would, and once your done with the HTML just put in ` – mike510a Nov 21 '16 at 11:14

2 Answers2

0

Check your code for starters...What is 20204040 doing sitting in there? That will cause errors, So you need to get rid of that...

In your echo '' and with your concatenation, you have this

'<img src="image/$image"/>'.

Change it to this...

'<img src="image/'.$image.'"/>'.

You did it everywhere else but not in this instance.

TimBrownlaw
  • 5,457
  • 3
  • 24
  • 28
0

You're storing the entire image in a blob column. So, you can't use this stored data as if it were a simple path to a image. So, no chance to do a simple: <img src="image/$image">.

You will need to "cast" the image through a php file that returns an image/jpg content-type, and use this php file as the path, like: <img src="image_builder.php?image_id=999.php">

See the accepted answer of this post: How can I store and retrieve images from a MySQL database using PHP? for more detailed information.

Btw, if you find this too complex, consider storing only the path to an image stored in disk. In that case you only need a varchar column, not a blob one. And so, you may use your approach of echoing <img src="image/$image_path">.

Community
  • 1
  • 1
leoap
  • 1,684
  • 3
  • 24
  • 32