Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Jonathan Morales
Jonathan Morales
40,046 Points

How to get image from database to display on web?

How can i use php to fetch image from database and show it on my php file? I am uploading a image directly to the localhost database. Each time i try to echo the image i only get a bunch characters and symbols. i tired by writing

while($row = mysqli_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['firstname'] . "</td>"; echo "<td>" . $row['lastname'] . "</td>"; echo "<td>" . $row['age'] . "</td>"; header("Content-type: image/jpeg"); echo '<td> <img src="data:image;base64,'.$row['img'].' "></td>'; // here i try to show image echo "</tr>"; }

but it does not work for me

1 Answer

Jonathan, I strongly suggest you not store images directly in a database.

Instead, just store their names as strings (e.g., test.png or test.jpg, etc.) in a database table's field and then use PHP to fill in the src attributes for your img tags.

Putting images directly into databases has some large drawbacks!