SQL查询问题:并非所有记录都从mysql数据库中的多个表中检索

时间:2016-08-05 14:59:32

标签: php html mysql sql

所以我有3张桌子。一个匹配表,其中包含两队相互比赛的记录。

mchID mchTeama mchTeamb mchScorea mchScoreb
1 Cathedral Holy Trinity 3 0
2 St. Andrew's Immanuel Church 2 0

我还有球队表,其中包含所有参赛队伍的记录 tmID tmName
1 Cathedral 2 Holy Trinity Lekki

玩家

plyID plyName plyPosition teamID 1 Michael Defense 2 2 Peter Forward 1 3 Chukwudi Forward 1 4 Johnson Midfield 2 5 John Forward 2 6 Samuel keeper 1

在我的html页面上,我有一个包含多个下拉列表的表单,根据传递给页面的匹配ID,根据对所有三个表的一系列查询从player表中填充。

$colname_matchrf = "-1";
    if (isset($_GET['mchrf'])) {
      $colname_matchrf = $_GET['mchrf'];
    }

    $query_match = "SELECT * FROM matches WHERE mchID = $colname_matchrf";
    $result_match = mysqli_query($connBiscup, $query_match);
    $row_match = mysqli_fetch_assoc($result_match);
    $totalRows_match = mysqli_num_rows($result_match);

    $matchTeamA =  $row_match['mchTeama'];
    $matchTeamB =  $row_match['mchTeamb'];


    $query_lineupteama = "SELECT * FROM teams WHERE tmName = '".$matchTeamA. "'";
    $result_lineupteama = mysqli_query($connBiscup, $query_lineupteama);
    $row_lineupteama = mysqli_fetch_assoc($result_lineupteama);
    $totalRows_lineupteama = mysqli_num_rows($result_lineupteama);

    $matchTeamaID =  $row_lineupteama['tmID'];

    $query_playersa = "SELECT * FROM players WHERE teamID = $matchTeamaID AND plyPosition != 'Coach' AND plyPosition != 'Assistant Coach'";
    $result_playersa = mysqli_query($connBiscup, $query_playersa);
    $row_playersa = mysqli_fetch_assoc($result_playersa);
    $totalRows_playersa = mysqli_num_rows($result_playersa);


    $query_lineupteamb = "SELECT * FROM teams WHERE tmName = '".$matchTeamB. "'";
    $result_lineupteamb = mysqli_query($connBiscup, $query_lineupteamb);
    $row_lineupteamb = mysqli_fetch_assoc($result_lineupteamb);
    $totalRows_lineupteamb = mysqli_num_rows($result_lineupteamb);

    $matchTeambID =  $row_lineupteamb['tmID'];  

    $query_playersb = "SELECT * FROM players WHERE teamID = $matchTeambID AND plyPosition != 'Coach' AND plyPosition != 'Assistant Coach'";
    $result_playersb = mysqli_query($connBiscup, $query_playersb);
    $row_playersb = mysqli_fetch_assoc($result_playersb);
    $totalRows_playersb = mysqli_num_rows($result_playersb);

这是表格下拉样本

<?php
    while ($row_playersa = mysqli_fetch_assoc($result_playersa)) {
        $playersa[] = $row_playersa;
    }
?>
<div class="std_textbox2">
  <select name="captaina" class="input-field-login3" id="captaina" tabindex="1">
    <option selected="Selected">--Select Player--</option>
      <?php foreach ($playersa as $playera): ?>
        <option value="<?php echo $playera['plyName']; ?>"><?php echo $playera['plyName']; ?></option>
      <?php endforeach ?>
  </select>
</div>

所以问题是这样,表格下拉列表会被填充,但不会包含我希望存在的所有记录。例如,team A的下拉列表应该让玩家表中的所有玩家的 teamID 1 。但我得到的只是一些记录并非全部。 任何关于错误的帮助都会高度赞赏。

2 个答案:

答案 0 :(得分:1)

您希望在您的where语句中使用AND &&&&执行其他操作。

  $query_playersa = "SELECT * FROM players WHERE teamID = $matchTeamaID and plyPosition != 'Coach' and plyPosition != 'Assistant Coach'";

还会将您的代码更改为不易受到注入攻击。您的网站将在数小时内完成。

答案 1 :(得分:0)

感谢大家的帮助。我发现了这个问题。在sql查询中,我已经调用了这个方法$row_playersa = mysqli_fetch_assoc($result_playersa);,这已经占据了第一行。因此,在循环while ($row_playersa = mysqli_fetch_assoc($result_playersa)) { $playersa[] = $row_playersa; }中再次调用该方法会占用第二条记录,依此类推。所以为了解决这个问题,我在查询中取出了这个方法。