php数组或sql错误

qojgxg4l  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(291)

我怀疑是数组错误或mysql查询错误。我已经打印了函数中的所有变量,它们返回正确的结果,并尝试了phpmyadmim中返回正确结果的相同查询。
用例非常简单
得到 $_GET['gameID'] 查询数据库
从第一次查询中获取返回的结果,第二次使用周数和sport查询db。
从第二个查询返回包含所有结果的数组

问题

当前函数只返回表的最后一行,而不是返回所有行。
表的图片

输出结果

代码

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

如果有人能给我的代码快速扫描和/或提供一些建议,将不胜感激。

gpnt7bae

gpnt7bae1#

您正在覆盖 $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
pkln4tw6

pkln4tw62#

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

相关问题