1

I've developed a PHP-code that takes in user's comments and stores them in DB. But I wanted to output the user's image also when he posts a comment. Also, if there is no image for the user in the DB, then a default image should be shown. Currently my PHP code looks like:

<?php
if($pid && $sess_uid && $comment_text){
    $sql = "INSERT INTO ttl_comments (comment_pid, comment_text, comment_uid) VALUES ('".$pid."', '".$comment_text."', '".$sess_uid."')";

    $result = mysql_query($sql);
    $comment_id = mysql_insert_id();
    if($comment_id > 0)
    echo '<div class="usercomment">
            <div class="clr"></div>
            <a href="profile.php?uid='.$sess_uid.'"></a>
            <a href="profile.php?uid='.$sess_uid.'">
                <span class="commentsuser">'.$sess_fname." ".$sess_lname.'</span>
            </a>
            <span class="comments">'.$comment_text.'</span>
        </div>
}
?>
user188995
  • 447
  • 4
  • 10
  • 17
  • 2
    What have you tried? I don't see anything here that's trying to place an image on the page. – ametren Jun 22 '12 at 17:28
  • @ametren: I was trying to use the `echo` command to output images but was having problems. I really don't know how to output images from a DB. – user188995 Jun 22 '12 at 17:29
  • 2
    You shouldn't be storing images in the db. At most you store the LOCATION of an image on your server's file system. – Marc B Jun 22 '12 at 17:31
  • If the image is stored in the DB as a BLOB then you'd have to show an `IMG` tag with a ref to that PHP script which dumps the image to the response as the `src` for that tag – ametren Jun 22 '12 at 17:31
  • No, no i'm not storing images in the DB. – user188995 Jun 22 '12 at 17:38

3 Answers3

1

I'm assuming that you're not storing the image data in the database as a BLOB. If you are I'm going to kindly ask you not to do that :)

Providing you have a reference to the location of the image stored in the DB like this

/images/users/user0001.jpg

Then you would simply get get that data as text and echo it as the source for an tag.

Kinda like this:

$row = mysql_fetch_result($result);

< img src ="< ?php echo $row['name_of_image_col']; ? >" >
BOMEz
  • 1,020
  • 14
  • 34
  • 1
    Yes. Thanks for the little hint. It works. I included: ` ` What should I do now as a user of SO? Should I **"Answer my Question"?** – user188995 Jun 22 '12 at 17:40
  • Well it looks like you selected my answer as correct, which is the norm. "Answer my Question" is there in case you don't get a suitable answer from other users, but found the answer through other sources.Welcome to stackoverflow, see the FAQ for more details on asking and answering questions: http://stackoverflow.com/faq – BOMEz Jun 22 '12 at 17:48
  • 1
    Thanks again. Your answer gave me an instinct to think. That's similar to being answered I guess. :P – user188995 Jun 22 '12 at 17:54
0

First of all, I recommend you store images on the filesystem and then just put the path to the image in the DB instead of storing the actual image in the DB.

However, if the image is stored in the DB as a BLOB then you'd have to show an IMG tag with a ref to that PHP script which dumps the image to the response as the src for that tag.

To dump the image from the DB you'd have to do something like this

ametren
  • 2,186
  • 15
  • 19
0

you should always store the instance of the image in the DB when working with larger amount of data. One cannot go on naming 1000 images. I guess the following link will help you improve upon it: store image in database or in a system file?

Community
  • 1
  • 1
xan
  • 4,640
  • 13
  • 50
  • 83