Report this

What is the reason for this report?

How to display BLOB type of image in webpage in php

Posted on February 24, 2021

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);
        $statement->execute();
        $statement->closeCursor();

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, news_post.author, 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);
$statement->execute();
$news = $statement->fetchAll();
$statement->closeCursor();

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) : ?>

                    <tr>
                        <td>
                            <?php echo $newsPost['newsID']; ?>
                        </td>
                        <td>
                            <?php echo $newsPost['news_link']; ?>
                        </td>
                        <td>
                            <?php echo $newsPost['news_title']; ?>
                        </td>
                        <!--td>
                            <div>
                                <datalist><//?php echo $newsPost['news_content']; ?></datalist>

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

                    </tr>



                <?php endforeach; ?>

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

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Start building today

From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.