PHP选择和内部连接表问题。

时间:2018-06-07 18:02:21

标签: php mysql

我试图让它获得事件的所有用户tournament_regid,但它只选择我的数据库中的第一个条目,然后当我根据该regid加入两个表时它是仅显示一个条目。 关于我如何能够让所有用户参加tournament_regid的任何想法?

这就是我目前所拥有的:

   <?

    $stmt = $DB_con->prepare('SELECT tournament_regid, paid FROM tournament_registration WHERE userid= :session_id');
        $stmt->bindParam(':session_id', $_SESSION['user']['id']);   
        $stmt->execute();
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
    {
        $regid = $row['tournament_regid'];
        ?>  

        <?
        $stmt = $DB_con->prepare("SELECT * FROM tournament_registration JOIN tournaments ON tournament_registration.tournament_regid = tournaments.tournamentid WHERE tournaments.tournamentid = :regid");
        $stmt->bindParam(':regid', $regid);
        $stmt->execute();
        while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
    {

            ?>

        <h1> Event Name: <? echo $row['tournament_name'];?> </h1>
        <h1> Event Date: <? echo $row['tournament_date'];?> </h1>
        <h1> Event Time: <? echo $row['tournament_time'];?> </h1>
        <h1> Event Desc: <? echo $row['tournament_dec'];?> </h1>
        <h1> Paid: <? echo $row['paid'];?> </h1>
        <h1> <? echo $row[''];?> </h1>
        <a href="viewevent.php?event_id=<?php echo $row['tournament_regid'];?>"> VIEW Event Details  </a>
        <h1>
        <?php echo $regid;?>
        </h1>
        <?
    }
    }
        ?>

更新了SQL查询:现在的问题是它看起来像是获得了所有人的资料?因为现在有很多数据显示。

<? 
$stmt = $DB_con->prepare("

SELECT 
   *
FROM
   users
INNER JOIN
    tournament_registration ON tournament_registration.userid = :userid
INNER JOIN
    tournaments ON tournaments.tournamentid = tournament_registration.tournament_regid
");
$stmt->bindParam(':userid', $_SESSION['user']['id']);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{

        ?>

1 个答案:

答案 0 :(得分:0)

“SELECT    tournament_registration.tournament_regid 从    用户 内部联接     tournament_registration ON tournament_registration.userid = users.userid 内部联接     比赛ON tournaments.tournamentid = tournament_registration.tournament_regid “

使用此更新的SQL查询来实现它