-3

我有一个关于向我的网站添加链接的问题,当“点击”时,该链接会将用户发送到存储在我的MySQL 数据库中的随机 URL 。

我的数据库的标题是“电影”,我希望能够让用户点击一个链接,它会将他们发送到存储在我的数据库中的电影页面 URL 之一。

例如:用户单击“随机”链接并从我的数据库中转到电影页面(我列出了大约 110 个带有唯一 ID 的 URL)。

我会使用类似的东西:

  • header('位置:'xxxxx);

要做到这一点?

我知道我应该能够使用 PHP 完成这项任务,但我只是无法弄清楚这一点。任何帮助都会很棒!谢谢。

4

2 回答 2

0

我假设您发布的查询选择了一部随机电影,并且存储 url 的字段称为“url”。

$result = mysql_query("SELECT url FROM movie ORDER BY RAND() LIMIT 0, 10")
                  or die(mysql_error());

$row = mysql_fetch_assoc($result);
mysql_free_result($result);
header('Location: '.$row['url']);

这应该重定向到数据库中的 url。

于 2013-05-14T21:12:34.687 回答
0

尝试这个:

mt_rand("", ""); // this creates random numbers and use it in a variable as:
$id= mt_rand("1", "110");
// hatever id number is generated it will put out that one in the query but it is only for one if you want multiple put $id in a for loop  than print the result in while loop.
$result = mysql_query("SELECT url FROM movie WHERE id= '$id' LIMIT 0, 10")
                  or die(mysql_error());

$row = mysql_fetch_assoc($result);
mysql_free_result($result);
header('Location: '.$row['url']);
于 2013-05-14T21:50:41.990 回答