gpt4 book ai didi

php - 使用 php 和 mysql 随机横幅显示

转载 作者:行者123 更新时间:2023-11-29 13:34:16 25 4
gpt4 key购买 nike

我正在创建我的网站 located here

我正在尝试添加一个在每次页面刷新时都会更改的横幅。我在我的数据库中设置了 2 个示例,称为“链接 1”和“链接 2”。当我得到它们时,我会想添加更多。

我想要发生的是这样的:

我想在我的网站上显示 2 张图像中的一张,当用户刷新页面时,它将选择这 2 张图像中的一张,并且每次页面刷新时都会继续执行此操作。

我正在一个名为banner.php的页面中对此进行测试,然后将其移动到我的footer.php并使其生效。

我目前在我的banner.php页面中有这段代码:

<?PHP
include_once('include/connection.php');

// Edit this number to however many links you want displaying
$num_displayed = 1 ;

// Select random rows from the database
global $pdo;
$query = $pdo->prepare ("SELECT * FROM banners ORDER BY RAND() LIMIT $num_displayed");
$query->execute();

// For all the rows that you selected
while ($row = execute($result))

{
// Display them to the screen...

echo "<a href=\"" . $row["link"] . "\">
<img src=\"" . $row["image"] . "\" border=0 alt=\"" . $row["text"] . "\">
</a>" ;
}
?>
<br /><br /><br />

但我收到此错误代码:

Fatal error: Call to undefined function execute() in banner.php on line 13

我的连接页面已被其他页面使用,因此我知道它可以正常工作。

请有人帮助我解决我做错的事情。

需要更多信息,请询问,我会将其添加到这篇文章中。

谢谢。

凯文

最佳答案

替换这个

while ($row = execute($result)) 

这样:

while ($row = $query->fetch())

编辑

这样可以更好地阅读。

while ($row = $query->fetch()) :
// Display them to the screen...
?>
<a href="<?php echo $row['link']; ?>">
<img src="<?php echo $row['image']; ?>" border="0" alt="<?php echo $row['text'];?>">
</a>
<?php endwhile; ?>
<br/>
<br/>
<br/>

关于php - 使用 php 和 mysql 随机横幅显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18961073/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com