为什么这个函数会返回一个结束= 1的行?

时间:2016-09-11 00:35:26

标签: php mysql mysqli

我的功能:

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的行。我无法弄清楚为什么。我错过了什么吗?

1 个答案:

答案 0 :(得分:-1)

仍然不确定为什么会这样做,但我只是通过将if更改为:

来修复它
    $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");

或者为所有人留空。