php/sql - phpmyadmin binary value show on web page

Asked

Viewed 32 times

0

imagephpimagemhtml

I have a database of books each book has its ISBN, title etc and even has an image, which I uploaded in the database for each book. but the problem is that the image does not appear, does the image echo but in reality does not appear (see the photos). The code I’m using to call up the database image is as follows:

 <?php
include('config.php');

$sql= "SELECT * FROM books WHERE category='computing'";
$r=mysqli_query($conn, $sql);
?>
<html>
    <head>
    </head>
    <body>
<table cellpadding="2" cellspacing="2" border="0">
<tr>
    <th>ISBN</th>
    <th> Title </th>
    <th> Author's name</th>
    <th> edition</th>
    <th> year</th>
    <th> category</th>
    <th> publisher</th>
    <th> quantity-in-stock</th>
    <th> price</th>
</tr>

<?php while($books =mysqli_fetch_object($r)){?>
    <tr>
    <td> <?php echo $books->ISBN;   ?></td>
    <td> <?php echo $books->Title;  ?></td>
    <td> <?php echo $books->Authorsname; ?></td>
    <td> <?php echo $books->edition;?></td>
    <td> <?php echo $books->year;   ?></td>
    <td> <?php echo $books->category;   ?></td>
    <td> <?php echo $books->publisher;  ?></td>
    <td> <?php echo $books->quantityinstock; ?></td>
    <td> <?php echo $books->price; ?></td>
    echo '<img src="data:image/jpeg;base64,'.base64_encode( $books->Image ).'"/>';
    <td> <a href="shoppingcart.php?ISBN=<?php echo $books->ISBN; ?>">Order Now</a></td>
    </tr>
    <?php } ?>
</table>
    </body>
</html>

The base I am using is phpmyadmin and it speaks "binary" please see the photo. I’m sorry if it’s a basic question, but I’m new at this and I don’t quite understand.

Thank you

1 answer

0


You made a little mistake using the function base64_decode instead da base64_encode. The base64_encode function converts your image data into binary so that it can be interpreted with MYME Base64 by the browser.

Correcting gets:

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

When you used base64_decode the browser could not interpret the content, because it was not a valid Base64.

  • hi, I tried but it didn’t work, I’ll put the whole code. so it might be something I’m not doing well

  • Note that the line that has the echo command is not inside the php tags (if it really is in your code, it must be printing the entire line in your table). I tested with png image (image/png) and had the behavior experienced. I don’t know the reason, but I’ve had trouble displaying images with the extension. JPG (uppercase instead of minuscule). Anyway try with a different mime.

  • Voce tested with php tags?

  • Yes, I put inside the php tags.

  • I tried and it worked, thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.