Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display an image from a MySQL database using PHP

Tags:

php

mysql

New to this but loving it. I have a database of URLs of pictures of cats that I want to display in a webpage. I wrote some php to do this but all I'm seeing it the URL of the image and not the image itself. Here is my full code:

<html>
<body>
<h1>Katz!!</h1>

<!--Connect to Database-->
<?php
$db_host = "localhost";
$db_username = "Alex";
$db_pass = "";
$db_name = "cats";

@mysql_connect("$db_host","$db_username","$db_pass") or die ("could not connect to mySQL");
@mysql_select_db("$db_name") or die ("No database");

$sql="SELECT * FROM cats_test";
$records = mysql_query($sql);
?>

<!--Populate table from database-->
<table  border="2" cellpadding="1" cellspacing="1">
<tr>
    <th>cat table</th>
</tr>
<?php
while($cats_test=mysql_fetch_assoc($records)) {
    echo "<tr>";
    echo "<td>".$cats_test['image']."</td>";
    echo "</tr>";
}
?>
</table>
</body>
</html>

As you can see, the database name is "cats" with a table called "cats_test" that holds all of the images in a column called "image" you can find a screenshot of that here

You can also see the URLs being displayed instead of images here

I've probably done something really stupid, so would appreciate any help you guys might have!!

like image 420
Nosmig Avatar asked Jul 16 '26 12:07

Nosmig


1 Answers

Replace your :

echo "<td>".$cats_test['image']."</td>";

With :

echo "<td><img src='".$cats_test['image']."' ></td>";
like image 156
Raphaël Vigée Avatar answered Jul 18 '26 03:07

Raphaël Vigée



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!