【问题标题】:How can I add the image so it will show the image not only the location?如何添加图像以便它不仅显示位置还显示图像?
【发布时间】:2017-08-05 03:54:22
【问题描述】:

您好,我是新手,我正在尝试弄清楚 PHP 和 MySQL 是如何工作的。 我正在尝试在页面上显示图像,但现在我只得到文本。

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

【问题讨论】:

  • 图片标签是&lt;img src=""&gt;它在任何html手册中都有描述。
  • @VictorSmt 如果您要编辑问题以格式化表格,请至少使用 ASCII 艺术工具,例如 ozh.github.io/ascii-tables
  • 这确实是 HTML 101 的东西,而不是 PHP/mysql 问题。在 PHP 之前学习 HTML 对任何网页设计师/开发人员来说都是必不可少的。

标签: php html mysql image


【解决方案1】:

你只是在输出文本:

$row["image"]

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

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

所以结果是:

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

【讨论】:

  • 感谢您对我的问题的回答非常感谢。
猜你喜欢
  • 2021-12-25
  • 1970-01-01
  • 1970-01-01
  • 2019-05-27
  • 2012-01-28
  • 2013-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多