mysql结果打印多个

knpiaxh1  于 2021-06-23  发布在  Mysql
关注(0)|答案(3)|浏览(330)

在我的数据库中,我有一个一对多的表关系,其中一个父母可以有很多孩子。主键是父母的电子邮件。我要带孩子们来

$results1 = mysqli_query($con,"
SELECT directory.email
     , dirKids.kname
     , dirKids.kbirthday 
  FROM directory 
  JOIN dirKids 
    ON '$row[email]' = dirKids.parent
");

然后我绕过去 echo 我的html页面的值

while($row1 = mysqli_fetch_array($results1)) {
    if (!empty($row1["kname"])) {
        echo "<tr><td>". $row1["kname"] ."</td><td>".
        $row1["kbirthday"]."</td></tr>";
    }
}

我遇到的问题是,我的数据库中只有一位家长有孩子,但它会打印孩子的名字和生日10次,因为我的数据库中有10个人。我怎样才能让它只打印一次孩子的名字和生日?
我的完整代码如下:

<?php
    $con = mysqli_connect("localhost", "username", "password", "db");
    // Check connection
    if (mysqli_connect_errno()) {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    $results = mysqli_query($con,"SELECT directory.id, directory.fname, directory.lname, directory.address, directory.bdname, directory.birthday, directory.cell, directory.email, directory.sFName, directory.sBirthday, directory.sCell, directory.sEmail FROM directory ORDER BY lname") or die ("couldn't fetch query");

    echo "<div class='accordion' id='accordion'>";

    // output data of each row
    while($row = mysqli_fetch_array($results)) {

    $results1 = mysqli_query($con,"SELECT directory.email, dirKids.kname, dirKids.kbirthday FROM directory JOIN dirKids ON '$row[email]' = dirKids.parent");

    echo "</table></div>";
    if ($row['sFName'] == "" || $row['sFName'] == "undefined") {
        echo "<div class='card'><div class='card-header'
        id='headingOne'><h5 class='mb-0'><button class='btn btn-link' 
        type='button' data-toggle='collapse' data-target='#collapse".
        $row["id"] ."' aria-expanded='true' aria-controls='collapse".
        $row["id"] . "'><h5>".$row["fname"] ."<span id='lnameText'>".
        $row["lname"] ."</span></h5></button></h5></div><div
        id='collapse". $row["id"] . "' class='collapse'
        aria-labelledby='headingOne' data-parent='#accordion'><div
        class='card-body'><table id='myUL' class='table'><tr></tr><tr>
        <td><h5>Address</h5></td><td>". $row["address"] ."</td></tr>
        <tr><td><h5>Birthday</h5></td><td>".$row["birthday"]."</td>
        </tr><tr><td><h5>Cell</h5></td><td>". $row["cell"]."</td></tr>
        <tr><td><h5>Email</h5></td><td>". $row["email"] ."</td></tr>
        </table></div>";

    echo "<div class='col-md-6'><h3>Children</h3><table class='table'><tr><th><h5>Name</h5></th><th><h5>Birthday</h5></th>";

    while($row1 = mysqli_fetch_array($results1)) {
        if (!empty($row1["kname"])) {
            echo "<tr><td>". $row1["kname"] ."</td><td>". $row1["kbirthday"]."</td></tr>";
        }
    }

    echo "</table></div></div>";
?>
stszievb

stszievb1#

您的查询应该如下所示;

$select = mysqli_query($db, "SELECT * FROM parents_database WHERE parent_name = '$parent_name'");
while ($row = mysqli_fetch_array($select, MYSQLI_ASSOC)) {
  // echo kids here..
}
xmd2e60i

xmd2e60i2#

不知道你需要什么。因为你发布了两个不同的查询。
但第一个有错误的方法,希望你需要解决这一个。
我想你的意思是:

SELECT directory.email
     , dirKids.kname
     , dirKids.kbirthday 
  FROM directory 
  JOIN dirKids 
    ON directory.email = dirKids.parent
 WHERE directory.email = '$row[email]'
ffscu2ro

ffscu2ro3#

由于第二个while循环所需的数据完全来自childs表,因此只需为此构建select语句,而忽略join和where语句,它们只查找父电子邮件。
下面的代码进入主while循环并替换

$results1 = mysqli_query($con,"SELECT directory.email, dirKids.kname, dirKids.kbirthday FROM directory JOIN dirKids ON '$row[email]' = dirKids.parent");

具有

//Build the select statement
$sql = "SELECT kname, kbirthday FROM dirKids WHERE parent = '" .$row[email] . "'";
//now run the query
$results1 = mysqli_query($con,$sql);
//uncomment the below to see the results
//var_dump(mysqli_fetch_array($results1));

相关问题