我遇到mysql_fetch_array的while函数问题。我已经尝试了在声明之后使用的内容以及我现在所做的比以前更好的工作。我以为我可以在彼此内部运行一大堆循环,但显然不是。我目前在前两个语句上都有大括号,而在其他语句中没有,你可以在代码中清楚地看到这一点。
然而,我现在拥有的意思是在每个语句之后有多个变量导致第二个变量在回显时停止工作等等。我试图避免使用数组作为变量将更容易布局。不知道这里发生了什么。我通常在每个语句后使用大括号,但这只会使整个事情变得多余。我应该怎么做以保持所有变量的工作?到目前为止,我对PHP还不是很好,感谢所有的帮助!
我只是为了将来的目的而乱七八糟,所以我知道我应该使用mysqli。我刚刚学会了mysqli,所以我只是使用了mysql,因为我觉得暂时感觉更舒服。
以下是代码:
//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 * FROM members WHERE artistname = '$favouritedArtist'")or die(mysql_error());
while ($updatedBio = mysql_fetch_array($fetchupdatedBio))
$updatedBio = $updatedBio['bio'];
//Get updates to profile pic
$fetchupdatedProfile = mysql_query ("SELECT * FROM members WHERE artistname = '$favouritedArtist'")or die(mysql_error());
while ($updatedProfile = mysql_fetch_array($fetchupdatedProfile))
$updatedProfile = $updatedProfile ['image1'];
//Get any new pictures
$fetchPic = mysql_query ("SELECT * 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 * 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'];
//asign all variables into an
echo $trackname;
}
}
答案 0 :(得分:2)
首先,你绝对不应该尝试SELECT *,而只是尝试你需要的内容。 喜欢:
SELECT picurl FROM pictures WHERE artistname = '$favouritedArtist'
而不是
SELECT * FROM pictures WHERE artistname = '$favouritedArtist'
在你的:
while ($tracks = mysql_fetch_array($fetchTracks))
$trackurl = $tracks['trackurl'];
$trackname = $tracks['trackname'];
有一个错误,因为只有在while语句后面有一条指令时才需要括号。
同意你的
while ($sessions = mysql_fetch_array($fetchSessions))
没有括号,如果有多条指令与while相关,则不能这样做。
虽然只有当您知道MySQL请求中有多个答案时才需要。由于可能只有一个拥有此用户名的用户,因此您不需要一段时间。
所有这些都是php和mysql开发的基础知识,一个简单的谷歌搜索会给你答案。 我想你可能需要阅读一些关于php和mysql基础知识的教程。