PHP数组或SQL错误迭代foreach循环

时间:2018-01-08 03:12:15

标签: php mysql arrays

我怀疑是数组错误或mysql查询错误。我已经打印了函数中的所有变量,它们返回正确的结果,并在phpmyadmim中尝试了相同的查询,返回正确的结果。

用例非常简单

  1. 获取$_GET['gameID']并查询数据库
  2. 从第一次查询和查询数据库中获取第二次返回的结果,包括周数和运动。
  3. 返回一个包含所有结果的数组,形成第二个查询
  4. 问题

    当前函数仅返回表的最后一行而不是返回所有行。

    表格图片

    enter image description here 输出结果

    enter image description here

    代码

    function displayTeamsByGameID($gameID)
    {
        global $db;
        //now prevents late submissions
        $sql = "SELECT * FROM bru_schedule WHERE gameID = :gameID";
        $stmnt = $db->prepare($sql);
        $stmnt->bindValue(':gameID', $gameID);
        $stmnt->execute();
        if ($stmnt->rowCount() > 0) {
            $games =  $stmnt->fetch();
            print $weekNum = $games['weekNum'];
            print $sport = $games['sport'];
        $sql = "SELECT * FROM bru_schedule WHERE weekNum = :weekNum AND sport = :sport";
        $stmnt= $db->prepare($sql);
        $stmnt->bindValue(':weekNum', $weekNum);
        $stmnt->bindValue(':sport', $sport);
        $stmnt->execute();
        if($stmnt->rowCount() > 0){
         $matches= $stmnt->fetchAll();
            foreach ($matches as $match) {
                 $match[] = array('gameID' => $match['gameID'], 'weekNum' => $match['weekNum'], 'gameTimeEastern' => $match['gameTimeEastern'], 'homeID' => $match['homeID'], 'visitorID' => $match['visitorID'], 'sport' => $match['sport'], 'venue' => $match['venue'], 'tournament' => $match['tournament'], 'spread' => $match['spread']);
    
                     }//foreach
                }//statment weekNUm Sport
        }//statment gameID
            if (!isset($match)) {
                return false;
            }
            return $match;
    }//function
    

    如果有人可以快速扫描我的代码并提供一些建议,我们将不胜感激。

2 个答案:

答案 0 :(得分:4)

您正在覆盖$match变量,因为您在foreach语句和foreach正文中使用了相同的变量来创建结果数组。使用其他变量。请参阅下面的代码。我为结果使用了一个新变量$result

function displayTeamsByGameID($gameID)
{
    global $db;
    //now prevents late submissions
    $sql = "SELECT * FROM bru_schedule WHERE gameID = :gameID";
    $stmnt = $db->prepare($sql);
    $stmnt->bindValue(':gameID', $gameID);
    $stmnt->execute();
    if ($stmnt->rowCount() > 0) {
        $result = array();
        $games =  $stmnt->fetch();
        print $weekNum = $games['weekNum'];
        print $sport = $games['sport'];
    $sql = "SELECT * FROM bru_schedule WHERE weekNum = :weekNum AND sport = :sport";
    $stmnt= $db->prepare($sql);
    $stmnt->bindValue(':weekNum', $weekNum);
    $stmnt->bindValue(':sport', $sport);
    $stmnt->execute();
    if($stmnt->rowCount() > 0){
     $matches= $stmnt->fetchAll();
        foreach ($matches as $match) {
             $result[] = array('gameID' => $match['gameID'], 'weekNum' => $match['weekNum'], 'gameTimeEastern' => $match['gameTimeEastern'], 'homeID' => $match['homeID'], 'visitorID' => $match['visitorID'], 'sport' => $match['sport'], 'venue' => $match['venue'], 'tournament' => $match['tournament'], 'spread' => $match['spread']);

                 }//foreach
            }//statment weekNUm Sport
    }//statment gameID
        if (!isset($result)) {
            return false;
        }
        return $result;
}//function

答案 1 :(得分:0)

function displayTeamsByGameID($gameID)
{
    global $db;
    //now prevents late submissions
    $sql = "SELECT * FROM bru_schedule WHERE gameID = :gameID";
    $stmnt = $db->prepare($sql);
    $stmnt->bindValue(':gameID', $gameID);
    $stmnt->execute();
    if ($stmnt->rowCount() > 0) {
        $games =  $stmnt->fetch();
        print $weekNum = $games['weekNum'];
        print $sport = $games['sport'];
    $sql = "SELECT * FROM bru_schedule WHERE weekNum = :weekNum AND sport = :sport";
    $stmnt= $db->prepare($sql);
    $stmnt->bindValue(':weekNum', $weekNum);
    $stmnt->bindValue(':sport', $sport);
    $stmnt->execute();
    if($stmnt->rowCount() > 0){
     $matches= $stmnt->fetchAll();
        foreach ($matches as $match) {
             // 【don't reassign value to $match, it's confused! Use an new variable name】
             $ret[] = array('gameID' => $match['gameID'], 'weekNum' => $match['weekNum'], 'gameTimeEastern' => $match['gameTimeEastern'], 'homeID' => $match['homeID'], 'visitorID' => $match['visitorID'], 'sport' => $match['sport'], 'venue' => $match['venue'], 'tournament' => $match['tournament'], 'spread' => $match['spread']);

                 }//foreach
            }//statment weekNUm Sport
    }//statment gameID
        if (!isset($ret)) {
            return false;
        }
        return $ret;
}//function