PHPnoob23 PHPnoob23 - 28 days ago 5
HTML Question

How can I add the image so it will show the image not only the location?

Hello I'm new with this and I'm trying to figure out how PHP and Mysql work.
I'm trying to show an image on the page but now I only get text.

in myphpadmin I have created the following table.

dvdshop2
.
with dvd that has 5 categories.

description text 255.
id int primary_key auto increment.
image varchar 255.
price decimal 10,2.
title varchar 100.


I have one movie in my database with a picture like this.

id title price description image
1 Star Wars 12.99 sci-fi movie /dvdshop2/images/sw.jpg


I run xampp as a sever where
dvdshop2
is the working folder in my htdocs folder.
I want the data from the database to show up with the picture
sw.jpg
not the text
/dvdshop2/images/sw.jpg
when I open the page in my browser.

Here is my code.

<!DOCTYPE html>
<html>
<head>

</head>
<body>

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";


$conn = new mysqli("127.0.0.1","root","","dvdshop2");

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT * FROM dvd";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["id"]. " - Title: " . $row["title"]. " " . "<br>" . "- Price:" . $row["price"] . "<br>" . " Description: " . " " . $row["description"] . " " . "<br>" . " image: " . "<br>" . $row["image"] . "<br>";
}
} else {
echo "0 results";
}

mysqli_close($conn);
?>

</body>
</html>


I hope somebody here can show me how its done because after 2 days I am still not able to succeed.

Thanks in advance,

Ray

Answer

You're just outputting the text:

$row["image"]

Instead, use an HTML img element where the text is the URL of the image:

'<img src="' . $row["image"] . '" />"

So the result would be:

<img src="/dvdshop2/images/sw.jpg" />