1

我有一个看起来像这样的数据库 -

图 1

我正在尝试根据时间获取前 10 个条目(time列中具有前 10 个值的条目)。我有以下代码。

    <?php
    include_once("connect.php");
    $sql = "SELECT * FROM scores order by time desc limit 10";
    $query = mysql_query($sql) or die("systemResult=Error");
    $counter    = mysql_num_rows($query);

    if($counter>0)
    {
        print("systemResult=Success");
        $array = mysql_fetch_array($query);

        foreach($array as $data)
        {
            $athleteName    = $data["athleteName"];
            $email = $data["email"];
            $time = $data["time"];
            $timeStamp = $data["timeStamp"];
            $country = $data["country"];

            print "&athleteName=" . $athleteName;
            print "&email=" . $email;
            print "&time=".$time;
            print "&timeStamp=".$timeStamp;
            print "&country=".$country;
         }
    }
    else
    {
        print("systemResult=Error");
    }
?>

我得到的输出是

systemResult=Success&athleteName=7&email=7&time=7&timeStamp=7&country=7&athleteName=7&email=7&time=7&timeStamp=7&country=7&athleteName=4&email=4&time=4&timeStamp=4&country=4&athleteName=4&email=4&time=4&timeStamp=4&country=4&athleteName=G&email=G&time=G&timeStamp=G&country=G&athleteName=G&email=G&time=G&timeStamp=G&country=G&athleteName=n&email=n&time=n&timeStamp=n&country=n&athleteName=n&email=n&time=n&timeStamp=n&country=n&athleteName=2&email=2&time=2&timeStamp=2&country=2&athleteName=2&email=2&time=2&timeStamp=2&country=2&athleteName=I&email=I&time=I&timeStamp=I&country=I&athleteName=I&email=I&time=I&timeStamp=I&country=I

可以看出,我得到的输出不是数据库表中的输出。我得到了奇怪的价值观。我究竟做错了什么?

4

2 回答 2

2

您不需要在您的情况下使用 for each,如果是这样,只需打印 $data,尝试删除 foreach 循环,如果您想获取所有记录,则使用 while:

 while($data = mysql_fetch_array($query))
    {
        $athleteName    = $data["athleteName"];
        $email = $data["email"];
        $time = $data["time"];
        $timeStamp = $data["timeStamp"];
        $country = $data["country"];

        print "&athleteName=" . $athleteName;
        print "&email=" . $email;
        print "&time=".$time;
        print "&timeStamp=".$timeStamp;
        print "&country=".$country;
     }
于 2013-03-10T11:06:17.200 回答
2

尝试

while($data = mysql_fetch_array($query)) {
    $athleteName    = $data["athleteName"];
    //...
于 2013-03-10T11:07:21.957 回答