之前我已经发布了这些代码,但之所以不同,因为我真的不知道发生了什么。
我有几个while循环从数据库中提取数据以形成最新信息的提要。在你问之前我会使用mysqli,但我现在只使用mysql所以我可以将循环排序,因为我觉得这样更舒服。
每个sql查询可以但不一定有几行数据,因此需要while循环。我还必须在最后将所有数据组织成一个数组,因此它可以按时间戳排序(时间戳不在sql查询中)。
我遇到的问题是,要将它全部组织到数组中,按照现在的方式,所有结束的大括号必须在数组之后,以便可以将每一位数据放入其中。由于这个原因,一些变量可以输出多次,因为每个sql查询中可以有不同数量的行。顺便说一下,支架的排列方式不是这样的。
在下面的代码中,第二批while循环的括号中的括号在变量后面是直的。这意味着表中只有一行输出到数组中。
我希望这可以解释它。这是我能做的最好的事情。如何安排while循环,以便第二批查询的EACH中的所有数据都可以输出到数组中?
<?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
}
}
?>
答案 0 :(得分:0)
您的代码应该从一开始就缩进 为了获得帮助,请至少帮助自己。
现在让我们看看问题出在哪里 如果我确实正确地解决了您的问题,请为每个请求使用临时数组 但是,如上所述,PDO为您提供了一个名为fetchAll的强大工具。你应该看一下。