简体   繁体   English

PHP数组或SQL错误遍历foreach循环

[英]PHP Array or SQL error iterating over foreach loop

I have what I suspect is either an array error or a mysql query error. 我怀疑是数组错误还是mysql查询错误。 I have printed all the variables in the function, they return the correct result, and tried the same query in phpmyadmim which returns the correct result. 我已经在函数中打印了所有变量,它们返回正确的结果,并在phpmyadmim中尝试了相同的查询,该查询返回了正确的结果。

The use case is pretty simple 用例非常简单

  1. Get $_GET['gameID'] and query DB 获取$_GET['gameID']并查询数据库
  2. Get returned result from 1st query and query DB for a 2nd time with week number and sport. 从第一个查询获取返回的结果,并第二次查询数据库的星期数和运动。
  3. return an array with all results form second query 返回第二个查询的所有结果的数组

PROBLEM 问题

The current function is only returning the last row of the table instead of returning all rows. 当前函数仅返回表的最后一行,而不返回所有行。

Picture of table 桌子图片

在此处输入图片说明 Output result 输出结果

在此处输入图片说明

Code

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

If anyone could give my code a quick scan and or provide some advice it would much appreciated. 如果有人可以快速扫描我的代码或提出建议,将不胜感激。

You are overwriting the $match variable since you have used the same variable in the foreach statement and within the foreach body for creating the result array. 您正在覆盖$match变量,因为您已经在foreach语句中和foreach主体中使用了相同的变量来创建结果数组。 Use a different variable. 使用其他变量。 See the code below. 请参见下面的代码。 I have used a new variable $result for the result. $result使用了新变量$result 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
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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM