0

我之前已经发布了代码,但是因为我真的不知道发生了什么,所以提出了不同的要求。

我有几个 while 循环从数据库中获取数据以形成最新信息的提要。在你问之前,我会使用 mysqli,但我现在只是使用 mysql,所以我可以对循环进行排序,因为我觉得这样更舒服。

每个 sql 查询可以但不一定有几行数据,因此需要一个 while 循环。我还必须在最后将所有数据组织到一个数组中,以便它们都可以按时间戳排序(时间戳不在 sql 查询中)。

我遇到的问题是将它全部组织到数组中,按照现在的方式,所有右大括号必须在数组之后,以便可以将每一位数据放入其中。由于这个原因,一些变量可能会被多次输出,因为每个 sql 查询中可能有不同数量的行。顺便说一下,括号暂时不是这样排列的。

在下面的代码中,第二批 while 循环中的每一个的括号都紧跟在变量之后。这意味着表中只有一行输出到数组中。

我希望这能解释它。这是我能做的最好的。如何安排 while 循环,以便将来自第二批查询的每个查询的所有数据输出到数组中?

<?php
    //fetch favourited artist(s)
    $fetchartistFavourite = mysql_query("SELECT * FROM artistfavourites WHERE username = '$username' AND password = '$pass';")or die(mysql_error());
    while ($artistFavourite = mysql_fetch_array($fetchartistFavourite)){

        $favouritedArtist = $artistFavourite['artistname'];
        $favouritedArtistUrl = $artistFavourite['artisturl'];

        //fetch favourite track(s)
        $fetchtrackFavourite = mysql_query ("SELECT * FROM trackfavourites WHERE username = '$username' AND password = '$pass'")or die(mysql_error()); 
        while ($trackFavourite = mysql_fetch_array($fetchtrackFavourite)){
            $favouritedTrack = $trackFavourite['artistname'];
            $favouritedTrackUrl = $trackFavourite['artisturl'];

            //Get news from favourited artist(s)
            //Get updates to bio
            $fetchupdatedBio = mysql_query ("SELECT bio FROM members WHERE artistname = '$favouritedArtist'")or die(mysql_error()); 
            while ($updatedBio = mysql_fetch_array($fetchupdatedBio)){
                $updatedBio = $updatedBio['bio'];
            }

            //Get any new pictures
            $fetchPic = mysql_query ("SELECT picurl FROM pictures WHERE artistname = '$favouritedArtist'")or die(mysql_error()); 
            while ($pic = mysql_fetch_array($fetchPic)){
                $pic = $pic['picurl'];
            }

            //Get any new tracks
            $fetchTracks = mysql_query ("SELECT * FROM tracks WHERE artistname = '$favouritedArtist'")or die(mysql_error()); 
            while ($tracks = mysql_fetch_array($fetchTracks)){
                $trackurl = $tracks['trackurl'];
                $trackname = $tracks['trackname'];
            }

            //Get any new gigs
            $fetchGigs = mysql_query ("SELECT * FROM gigs WHERE artistname = '$favouritedArtist'")or die(mysql_error()); 
            while ($gigs = mysql_fetch_array($fetchGigs)){
                //arrange gig data into format to be echoed
                $gig = $favouritedArtist.' is playing for the gig ' .$gigs['gigname'].' at ' .$gigs['venue'].' on the '.$gigs['day'].'th of '.$gigs['month'].', '.$gigs['year'];
            }

            //Get any new sessions
            $fetchSessions = mysql_query ("SELECT title FROM sessions WHERE artistname = '$favouritedArtist'")or die(mysql_error()); 
            while ($sessions = mysql_fetch_array($fetchSessions)){
                $sessionName = $sessions ['title'];
            }

            //Get new tracks from favourited tracks(s)if the artist has not been favourited
            $fetchnewTrack = mysql_query ("SELECT * FROM tracks WHERE artistname = '$favouritedTrack' AND artistname !='$favouritedArtist'")or die(mysql_error()); 
            while ($newTrack = mysql_fetch_array($fetchnewTrack)){
                $trackname2 = $newTrack['trackname'];
                $trackurl2 = $newTrack['trackname'];
            }

            //asign all variables into an array
            //echo array

        }
    }
?>
4

1 回答 1

0

您的代码从一开始就应该缩进。
为了得到帮助,请至少让自己得到帮助。

现在让我们看看问题出在哪里。
如果我确实正确地解决了您的问题,请为您的每个请求使用临时数组。
但是,如前所述,PDO 为您提供了一个名为 fetchAll 的强大工具。你应该看看。

( http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers )

于 2012-07-30T01:32:17.133 回答