2011-01-22 168 views
0

我有下面的代碼有問題 -幫助PHP While循環

mysqli_select_db($connect,"publishers"); 
//using for the display of the offer box 
$sql = "SELECT * FROM revbut WHERE onpublisher='$u'"; 

$g = mysqli_query($connect,$sql) or die(mysqli_error($connect)); 




mysqli_select_db($connect,"users"); 
//using for the display of the offer box 
$db = "SELECT * FROM button WHERE sessionusername='$u'"; 

$s = mysqli_query($connect,$db) or die(mysqli_error($connect)); 

$user = mysqli_fetch_assoc($s); 
while($geez = mysqli_fetch_assoc($g)) 
{ 
if($geez['onuser']) { 
    $globalname = $geez['sessionusername']; 
    include('js.php'); 
} 

else if($user['sessionusername']){ 
    include('js2.php'); 
} 

else { 
    echo "Nothing in here."; 
} 
} 

如果我刪除了while循環,它的工作原理。

+0

你的意思是你只刪除`while(...)`,或while循環塊內的所有內容? – BoltClock 2011-01-22 18:17:04

+2

你有什麼問題? – 2011-01-22 18:17:32

回答

1

如果您只有一條記錄,則根本不輸入while循環,代碼也不會執行,即使是第一條記錄也不會執行。這意味着,即使設置了$user['sessionusername'],也不包括js2.php,只要您沒有第二條記錄。

我不確定這是不是你的問題,因爲我不知道你想要達到什麼目的,但它讓我感到可能的缺陷。

[編輯]

if ($user = mysqli_fetch_assoc($s)) 
{ 
    if($user['onuser']) 
    { 
    $globalname = $user['sessionusername']; 
    include('js.php'); 
    } 
    else /* not needed: if($user['sessionusername']) */ 
    { 
    include('js2.php'); 
    } 
} 
else { 
    echo "Nothing in here."; 
} 

[EDIT2]

if ($user = mysqli_fetch_assoc($s)) 
{ 
    while ($user !== false) 
    { 
     if($user['onuser']) 
     { 
      $globalname = $user['sessionusername']; 
      include('js.php'); 
     } 
     else /* still not needed: if($user['sessionusername']) */ 
     { 
      include('js2.php'); 
     } 
     $user = mysqli_fetch_assoc($s); 
    } 
} 
else 
{ 
    echo 'Nothing to see here'; 
} 
0

意見的說明 -

if(mysqli_num_rows($g) > 1) 
    { 
     while($geez = mysqli_fetch_assoc($g)) 
     { 
      if($geez['onuser']) { 
       $globalname = $geez['sessionusername']; 
       include('js.php'); 
      } 

      else if($user['sessionusername']){ 
       include('js2.php'); 
      } 

      else { 
       echo "Nothing in here."; 
      } 
     } 
     } else { 
      if($geez['onuser']) { 
       $globalname = $geez['sessionusername']; 
       include('js.php'); 
      } 

      else if($user['sessionusername']){ 
       include('js2.php'); 
      } 

      else { 
       echo "Nothing in here."; 
      } 
    } 

當然,即使對我來說,這似乎是一個不錯的方法做它。您的while()聲明應該起作用。你發佈了所有的代碼嗎?