【问题标题】:Random banner display using php and mysql使用 php 和 mysql 随机显示横幅
【发布时间】:2013-09-23 13:52:22
【问题描述】:

我正在创建我的网站located here

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

我想要发生的事情是这样的:

我想在我的网站上显示 2 张图片中的一张,当用户刷新页面时,它将选择 2 张图片中的一张,并且每次页面刷新时都会继续。 p>

我在一个名为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 />

但我收到此错误代码:

致命错误:在第 13 行的banner.php 中调用未定义函数 execute()

我的连接页面被其他页面使用,所以我知道它有效。

请有人帮助我解决我做错了什么。

需要更多信息,请询问,我会将其添加到此帖子中。

谢谢。

凯夫

【问题讨论】:

    标签: php mysql content-management-system banner


    【解决方案1】:

    替换这个

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

    【讨论】:

    • 这样会去掉错误信息但不显示图片
    • OK 在 while 循环中使用 add print_r($row); 并查看是否获得数据数组,同时检查图像的路径。你看到屏幕上的 a 元素了吗?
    • 这会在顶角显示一个 _。单击时会刷新。
    • 查看我对答案的编辑,使用该代码显示您的图像,这应该可以工作!还可以使用 phpmyadmin 或其他方式检查您的查询,以确保查询有效。
    猜你喜欢
    • 2012-05-23
    • 2020-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-29
    相关资源
    最近更新 更多