Nosmig
Nosmig

Reputation: 165

Display an image from a MySQL database using PHP

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!!

Upvotes: 3

Views: 65

Answers (1)

Rapha&#235;l Vig&#233;e
Rapha&#235;l Vig&#233;e

Reputation: 2045

Replace your :

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

With :

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

Upvotes: 3

Related Questions