S. Mateijsen
S. Mateijsen

Reputation: 63

PHP MySQL Showing image from Database with the right id

Im trying to make a blog system for school. Ive almost got everything working, the only thing i need to let work is showing the images from my database with the right post. Right now i only get the image_name to show up instead of the image.

This is what the database table of the image looks like: image table

And here the blog post table: blog post table

Here is a picture of the row i got in the table post, the thing i want to accomplish is to have the image with the right image_id to be with the post with the accompanying post. post table row

The php code:

<?php
//get the posts from the database
    $query = $db->prepare("SELECT post_id, title, LEFT(body, 150) AS body, category, name FROM posts 
                            INNER JOIN categories ON categories.category_id=posts.category_id
                            INNER JOIN `blob` ON blob.image_id=posts.image_id
                            order by post_id DESC limit $start, $per_page");
    $query->execute();
    //make variabels
    $query->bind_result($post_id, $title, $body, $category, $image);

    ?>

Here the other part of the code where the image should come with the right post_id

<?php
                while($query->fetch()): 
                $lastspace = strrpos($body, ' ');
            ?>
            <article>
                <div class="image_small"><?php echo $image ?></div>
                    <div class="text_small"><h3><?php echo $title?></h3><br>
                    <p><?php echo substr($body, 0, $lastspace)?></p>
                    </div>
                <div class="vak"><h4>Vak: <?php echo $category?></h4></div>
                <div class="more_button"><?php echo "<a href='post.php?id=$post_id'><img src='images/more-button.png'  width='70' height='30' border='0'></a>"?></div>

            </article>

            <br><br>
            <?php endwhile;?>

Upvotes: 1

Views: 893

Answers (1)

RightClick
RightClick

Reputation: 1112

You are getting the image name instead of the image because you are querying the image name. Change your query and I think the rest will work.

$query = $db->prepare("SELECT post_id, title, LEFT(body, 150) AS body, category, image FROM posts 
                            INNER JOIN categories ON categories.category_id=posts.category_id
                            INNER JOIN `blob` ON blob.image_id=posts.image_id
                            order by post_id DESC limit $start, $per_page");

There are a couple ways to use that blob to show the image. I like this method...

echo '<img src="data:image/jpeg;base64,'.base64_encode( $image ).'"/>';

Upvotes: 3

Related Questions