【发布时间】:2013-12-22 22:48:21
【问题描述】:
我正在尝试让这个 html 页面 (display.html) 显示来自我的 MySQL 表中标记为“submissions”的随机图像提交和随机文本提交。
这是 display.html 的当前代码:
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>MIXED MESSAGES</title>
</head>
<?php
//This is the directory where images will be saved
$target = "images/";
$target = $target . basename( $_FILES['photo']['name']);
//This gets all the other information from the form
$name=$_POST['name'];
$pic=($_FILES['photo']['name']);
// Connects to your Database
mysql_connect("host", "username", "password") or die(mysql_error()) ;
mysql_select_db("voiid") or die(mysql_error()) ;
?>
<body>
<?php
$qry = mysql_query("SELECT * FROM submissions ORDER BY RAND() LIMIT 1");
while($rand = mysql_fetch_assoc($qry)){
echo $rand['name'];
echo $rand['pic'];
}
?>
</body>
</html>
目前页面正确显示文本,但不是显示随机图像,而是显示随机图像文件名 I.E hello5650_707133731307_3064319_n.jpg。我也想在文本和图像之间休息一下。
文本和照片字段都是 VARCHAR(200)。不确定我是否应该使用不同的数据类型。
非常感谢任何帮助!
【问题讨论】:
-
尝试将
<img src...添加到您的echo $rand['pic'];中,对于您的换行符,请使用echo $rand['name'] . "<br>";等。 -
按 rand() 排序非常有趣和游戏,除非您尝试在大型数据库上执行相同的操作。如果您做过流量密集型的事情,请记住这一点。
-
结合我的上述评论,如果您的文件不在同一个文件夹中,您可能需要添加
http://www.yoursite.com/images/,具体取决于您的图像存储位置。 -
谢谢 - 完整的代码行会是什么样子?
-
yoursite.com/images ... 这会导致显示数据库中的每个图像。我必须在 /images/... 之后添加什么?谢谢! @Fred-ii-
标签: php html mysql database random