2013-02-10 127 views
0

我想弄清楚如何在另一個準備好的staments循環中運行第二個準備好的語句。不是最佳的,但不知道我還能怎麼解決這個...準備好的語句嵌套在另一個準備的語句

這裏就是我想:

$stmt = $mysqli->stmt_init(); 

### GET THE CURRENT USERS FRIENDS ### 
$sql = "SELECT f.fk_user_id2 
      , u.username 
      , u.profileimg FROM ".$prefix."_friends f 
       INNER JOIN ".$prefix."_users u 
        ON f.fk_user_id2 = u.id  
      WHERE f.fk_user_id1 = ? ORDER BY u.username ASC"; 

if($stmt->prepare($sql)){ 

    $stmt->bind_param('i',$playerid); 
    $stmt->execute(); 
    $stmt->bind_result($userPoints); 
    while ($stmt->fetch()){ 

     $friendid = $row1['fk_user_id2']; 
     $friendname = $row1['username']; 
     if(empty($row1['profileimg'])){ 
      $profileimg = 'http://duefmun.dk/profileimages/noimage.png'; 
     } else { 
      $profileimg = 'http://duefmun.dk/profileimages/'.$row1['profileimg']; 
     } 

     ### GET THE HISTORY ### 
     $sql1 = mysql_query("SELECT wins, games FROM ".$prefix."_newversus WHERE fk_player=? AND fk_opponent=?"); 
     if($stmt->prepare($sql1)){ 
      $stmt->bind_param('ii',$playerid,$friendid); 
      $stmt->execute(); 
      $stmt->bind_result($wins,$games); 
      $stmt->fetch(); 

      if($games == 0){ 
       $history = $languagestring[165]; 
      } else { 
       $history = $languagestring[168].' '.$wins.' '.$languagestring[169].' '.$games.' '.$languagestring[170]; 
      } 
     } 

     $string .= $friendname.','.$friendid.','.$history.','.$profileimg.'^'; 

    } 
} 

$string = substr($string,0, (strlen($string)-1)); 
echo $string.'%'; 

$stmt->close(); 

也許有可能innerjoin第二個語句,但不知道怎麼樣。請幫忙在此先感謝: -/

+0

爲什麼'朋友'和'newversus'綁定到表名?這相當於使用關係數據庫的目的。 – ethrbunny 2013-02-10 13:02:48

回答

0

這不是一份準備好的聲明是你的問題,而只是一個變量
你只是重寫你的變量,在這種情況下$語句。
給它第二個循環的另一個名稱(並檢查其他變量invdved)

+0

好吧,但這又如何:$ stmt = $ mysqli-> stmt_init();我應該每次都開始一個新的? – Mansa 2013-02-10 15:26:36