Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.


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!