0

我在从 mysql 检索数据以显示在 marquee 中时遇到问题,当我转到我的网页时,它仅显示 marquee 中数据库中的一个数据。我的问题是如何检索所有存储的数据。谢谢下面是代码部分:

$select="SELECT newsid, headlines from news WHERE uploaddate order by uploaddate desc limit 4";
$rsd=mysql_query($select);

while($row = mysql_fetch_array($rsd))
{
    $newsid=$row['newsid'];
    $tittle=$row['headlines'];
    $uploaddate=$rowdata['uploaddate'];
}
4

3 回答 3

1

“只有一个数据”是什么意思?

也许问题就在这里

while($row = mysql_fetch_array($rsd))
{
    $newsid=$row['newsid'];
    $tittle=$row['headlines'];
    $uploaddate=$rowdata['uploaddate'];
}

这样,您将一遍又一遍地覆盖变量,因此在循环结束时,您将仅获得最后一条记录值。

一种可能的解决方案是制作一个(数组)数组并将数据存储到其中。

所以

$result = array()
while($row = mysql_fetch_array($rsd))
{
    $result[] = array('news_id' => $row['newsid'],
                      'title' => $row['headlines'],
                      'upload_date' => $rowdata['uploaddate']);
}
于 2012-06-13T12:28:35.117 回答
0

你试试这个..

 while($row = mysql_fetch_array($rsd))
    {
        $newsid=$row['newsid'];
        $tittle=$row['headlines'];
        $uploaddate=$rowdata['uploaddate'];
        $cont.="<a href='newpage.php?$newsid'>$title - $uploaddate</a>";
    }
    echo "<marquee>$cont</marquee>";
于 2012-06-13T12:36:42.000 回答
0

最有可能的问题是您一遍又一遍地写入相同的变量

while($row = mysql_fetch_array($rsd))
{
    $newsid=$row['newsid'];
    $tittle=$row['headlines'];
    $uploaddate=$rowdata['uploaddate'];
}

你最终会得到最后一个值newsid、标题和上传日期,所以我建议你在循环中回显数据。例子:

while($row = mysql_fetch_array($rsd))
{
    $newsid=$row['newsid'];
    $tittle=$row['headlines'];
    $uploaddate=$rowdata['uploaddate'];
    printf('<a href="/news.php?story=%d">%s</a> - on %s',$newsid,$title,$uploaddate);
}

或者将数据存储到数组中

$stories = array();
while($row = mysql_fetch_array($rsd))
{
    $stories[] = $row;
}
于 2012-06-13T12:30:55.200 回答