2016-11-11 69 views
-1

你好,我是新来的,我试图弄清楚PHP和MySQL是如何工作的。 我试图在页面上显示图像,但现在只能显示文本。如何添加图像,以便它不仅显示图像的位置?

in phpMyAdmin我创建了下表。

dvdshop2。 与有5个类别的DVD。

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

我在我的数据库中有一个电影,这样的照片。

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

我运行XAMPP的服务器,其中dvdshop2是我htdocs文件夹的工作文件夹。 当我在浏览器中打开页面时,我想要数据库中的数据显示图片sw.jpg而不是文本/dvdshop2/images/sw.jpg

这是我的代码。

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

图片标签是''它在任何html手册中都有描述。 –

+0

您需要学习更多html:https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img – aynber

+0

@VictorSmt如果您要编辑问题以格式化表格最少使用ASCII艺术工具,如https://ozh.github.io/ascii-tables/ – Styphon

回答

1

你只是在输出文本:

$row["image"] 

相反,使用HTML img元素,其中的文本是图像的URL:

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

那么结果会be:

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

谢谢你对我的问题的回答,真的很感激。 – PHPnoob23

相关问题