查询以显示数据库中的四个随机数据 [英] query to display four random data from database

查看:88
本文介绍了查询以显示数据库中的四个随机数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的php代码,用于显示数据库中的数据.我试图显示表中的随机数据.

This is my php code for displaying the data from the database. I am trying to display the random data from table.

<?php
include('connection.php');
$query="SELECT * FROM `banner_ad` ORDER BY RAND() LIMIT 4";
if($query_run=mysql_query($query))
{
    $i=4;
    $rows=mysql_fetch_array($query_run);
    while($rows)
    {
        echo $rows['banner_no'];
        echo $rows['banner_name'];
        echo "<a href=\"".$rows['Banner_website_url']. "\">";
        echo "<img src=\"".$rows['banner_image_url']."\" width=\"100px\" height=\"100px\">";
        echo"</a>";
    }
} else {
    echo'<font color="red"> Query does not run. </font>';
}
?>

但是此代码的问题是:

它什么也没显示.但是每当我尝试在上面的代码中进行一些修改时,例如:

It is displaying nothing. But whenever I am trying to make a little modification in the above code like:

<?php
include('connection.php');
$query="SELECT * FROM `banner_ad` ORDER BY RAND() LIMIT 4";
if($query_run=mysql_query($query))
{
    $i=4;
    $rows=mysql_fetch_array($query_run);
    while($rows && $i<4)
    {
        echo $rows['banner_no'];
        echo $rows['banner_name'];
        echo "<a href=\"".$rows['Banner_website_url']. "\">";
        echo "<img src=\"".$rows['banner_image_url']."\" width=\"100px\" height=\"100px\">";
        echo"</a>";
        $i=$i-1;
    }
} else {
    echo'<font color="red"> Query does not run. </font>';
}
?>

它显示4次相同的单个输出.但是它必须显示四个不同的输出.所以,请告诉我错误在哪里...以及我应该如何显示四个不同的随机输出.

It is displaying the same single output 4 times. But It has to display the four different output. So, Please tell me where is the bug ... And how am i suppose to display four different random output.

任何帮助将不胜感激 预先感谢

Any help will be appreciated Thanks in advance

推荐答案

您的第一个查询很好,但是while错误:

Your first Query is fine, but the while is wrong:

只要看看您在这里所做的事情即可

Just look at what you did here:

$rows=mysql_fetch_array($query_run);
while($rows)
{
    echo $rows['banner_no'];
    echo $rows['banner_name'];
    echo "<a href=\"".$rows['Banner_website_url']. "\">";
    echo "<img src=\"".$rows['banner_image_url']."\" width=\"100px\" height=\"100px\">";
    echo"</a>";
}

这将以无限循环"结尾,原因是始终设置$rows. 您需要的是:

this will end in an "infinite Loop" cause $rows will always be set. What you need is:

while($rows=mysql_fetch_array($query_run))

这将导致每次检查while条件时,myslq_fetch_array返回一个新行.如果返回所有4行,则$rows将为false,并且循环将停止.

this will cause myslq_fetch_array to return a new line everytime the while condition is checked. And if all 4 rows are returned, $rows will be false and the loop is stoped.

要完成: 在第二个示例中,您恰好在SAME行上进行了4次迭代,您只是通过调用myslq_fetch_array提取了一次.

And to be complete: In your second Example you are exactly iterating 4 times over the SAME row, you just fetched one time by calling myslq_fetch_array.

对此的一种可能的解决方案是在while循环内再次获取该行:

A possible solution to that will be to fetch the row again INSIDE the while-loop:

$i=4;
while ($i>0){
    $rows = mysql_fetch_array(...);
    $i--;
}

但是,您应该首选第一种解决方案,因为那样您就不必担心结果计数与您的迭代器变量匹配.

However you should prefer the first solution, because then you dont need to take care that the result count matches your iterator variable.

sidenode:将其命名为$row,而不带's',因为您总是只会返回一个行.

sidenode: Call it $row without the 's', because you always just getting ONE row back.

这篇关于查询以显示数据库中的四个随机数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆