How to display BLOB type of image in webpage in php

I want to ask about how to display blob type image in the php webpage. I tried too many ways , but It’s still seem like no way out.

–Here is the code that I storage the image type:

$URL = $_POST['link'];
        $title = $_POST['title'];
        $content = $_POST['textEditor'];
        // image format
        $avatar = $_POST['image'];
        $author = $_SESSION['username'];
        $active = $_POST['active'];
        $dateTime = $_POST['datetime'];
        $category = $_POST['types'];
        $query = "INSERT INTO news_post(news_link, news_title, news_content, news_image, author, status, create_date, category) VALUES("
                . ":link, :title, :textEditor, :image, :author, :active, :datetime, :types);";
        $statement = $conn->prepare($query);
        $statement->bindValue(':link', $URL);
        $statement->bindValue(':title', $title);
        $statement->bindValue(':textEditor', $content);
        $statement->bindValue(':image', $avatar);
        $statement->bindValue(':author', $author);
        $statement->bindValue(':active', $active);
        $statement->bindValue(':datetime', $dateTime);
        $statement->bindValue(':types', $category);

It works good and storage the image file into the database, and the image field that show up the bytes of the images.

Then, I want to display all the news table on webpage

— here is my code that I selected all the data from db:

$query = "SELECT news_post.newsID, news_post.news_link, news_post.news_title, news_post.news_content, news_post.news_image,, status_pots.status , news_post.create_date, category.category FROM ((news_post INNER JOIN status_pots ON news_post.`status`=status_pots.statusID) INNER JOIN category ON news_post.category=category.typeID )";
$statement = $conn->prepare($query);
$news = $statement->fetchAll();

Everything display as perfect except the image field, It could not retrieve data from BLOB and display it on the table on the page.

And the code below is my table data that show on page:

<?php foreach ($news as $newsPost) : ?>

                            <?php echo $newsPost['newsID']; ?>
                            <?php echo $newsPost['news_link']; ?>
                            <?php echo $newsPost['news_title']; ?>
                                <datalist><//?php echo $newsPost['news_content']; ?></datalist>

                            <img src="<?php  echo $newsPost['news_image'];?>" width="75" height="50"/>
                            <?php echo $newsPost['author']; ?>
                            <?php echo $newsPost['status']; ?>
                            <?php echo $newsPost['create_date']; ?>
                            <?php echo $newsPost['category']; ?>


                <?php endforeach; ?>

Can you guys help me out the way can display image on page. I really appreciate it and thank you so much!

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Hi there,

One option to do so is to use the following approach by base64 encoding the blo:

<img src="data:image/jpeg;base64,<?php  echo base64_encode($newsPost['news_image']);?>" width="75" height="50"/>

However, you need to keep in mind that this is generally not a very good approach s it will put too much load on the initial request and slowing the entire page down.

A better approach would be to store in your database only the path to the image and upload your images to your public folder.

Hope that this helps. Regards, Bobby