2016-09-11 125 views
-1

我的功能:爲什麼這個函數返回一個結束= 1的行?

function fetchAllGames($limit=9, $page=1, $isEnded="") 
{ 
    global $mysqli,$db_table_prefix; 

    if($isEnded!=""){$myWhere="WHERE ended=".$isEnded." ";}else{$myWhere="";} 
    $offset=($page - 1) * $limit; 
    $stmt = $mysqli->prepare("SELECT 
     created_timestamp, 
     end_timestamp, 
     winner_userid, 
     ended, 
     name, 
     game_uniqID, 
     game_id, 
     img 
     FROM ".$db_table_prefix."game 
     ".$myWhere." 
     LIMIT ".$limit." 
     OFFSET ".$offset); 
    $stmt->execute(); 
    $stmt->bind_result($created_timestamp, $end_timestamp, $winner_userid, $ended, $name, $game_uniqID, $game_id, $gameImg); 
    while ($stmt->fetch()){ 
     $row[] = array('created_timestamp' => $created_timestamp, 'end_timestamp' => $end_timestamp, 'winner_userid' => $winner_userid, 'ended' => $ended, 'name' => $name, 'game_uniqID' => $game_uniqID, 'game_id' => $game_id, 'gameImg' => $gameImg); 
    } 
    $stmt->close(); 
    if (isset($row)){ 
     return ($row); 
    } 
} 

如果我通過$allGames = fetchAllGames(9,1,0);叫它然後返回止帶= 0和行,結束= 1兩行,如果我通過$allGames = fetchAllGames(9,1,1);叫它它結束= 1只返回行。我無法弄清楚爲什麼。我錯過了什麼嗎?

+0

因爲0 =「」在劇組結束後,它拿起'else {$ myWhere =「」;}' – Drew

+0

你能澄清一點嗎? –

+0

不合邏輯,沒有。但是你的代碼似乎就是這種情況。如果你傳遞0,你可以vardump'$ myWhere'嗎? – Drew

回答

-1

仍然不知道爲什麼它這樣做,但我只是改變了,如果要固定它:

$myWhere = ""; 
    if($isEnded=="active"){$myWhere="WHERE ended=0";}elseif($isEnded=="ended"){$myWhere="WHERE ended=1";} 

而且通過

$allGames = fetchAllGames(9,1,"active"); 

或者

$allGames = fetchAllGames(9,1,"ended"); 

或調用它所有這些都留空。