2012-07-29 49 views
0

我對mysql_fetch_array的while函數有問題。我已經在聲明之後試用了什麼,現在我的工作比以前更好。我以爲我可以在彼此內部運行一個循環,但顯然不是。我現在在前兩個語句中有大括號,其他都沒有,你可以在代碼中清楚地看到。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

究竟是什麼問題?你不應該使用mysqli。你應該使用PDO – karka91 2012-07-29 17:38:00

+0

我改變了一些問題,但基本上我不知道發生了什麼,但似乎有幾個while語句停止變量的工作。我對PDO和mysqli以及mysqli進行了大量研究,這是該項目的更好選擇 – 2012-07-29 17:42:28

回答

2

首先,你一定要儘量不要選擇*,但你只需要的內容。 像:

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 ($sessions = mysql_fetch_array($fetchSessions)) 

沒有括號,你不能這樣做,如果有一個以上的相關指令時。

雖然只有當你知道在你的MySQL請求中會有多個答案時才需要。由於這個用戶名可能只有一個用戶,所以你不需要一段時間。

所有這些都是php和mysql開發的基礎知識,簡單的谷歌搜索會給你答案。 我想你可能需要閱讀一些關於php和mysql的基礎知識的教程。