我怀疑是数组错误或mysql查询错误。我已经打印了函数中的所有变量,它们返回正确的结果,并在phpmyadmim中尝试了相同的查询,返回正确的结果。
用例非常简单
$_GET['gameID']
并查询数据库当前函数仅返回表的最后一行而不是返回所有行。
表格图片
代码
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
如果有人可以快速扫描我的代码并提供一些建议,我们将不胜感激。
答案 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