sql查询在表中显示时丢失数据

lb3vh1jj  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(361)

**结案。**此问题不可复制或由打字错误引起。它目前不接受答案。
**想改进这个问题吗?**更新问题,使其成为堆栈溢出的主题。

两年前关门了。
改进这个问题
我有多个查询在phpmyadmin中生成正确的结果,但是当我将结果放入表中时,缺少数据。我的代码如下:

//set up the database 
$servername = "localhost";
$usernameDB = "root";
$passwordDB = "";
$nameDB = "teacheasy";
//connect to the database
$connection = new mysqli($servername, $usernameDB, $passwordDB, $nameDB);

$sql = "SELECT CONCAT(s.last_name,', ',s.first_name) AS 'StudentName', group_concat(a.date_absence) AS 'DatesAbsent', COUNT(a.student_id) AS 'TotalAbsent' \n"
. "FROM `students` s JOIN `absences` a ON s.student_id = a.student_id \n"
. "WHERE s.teacher_id='" . $_SESSION['userId'] . "'GROUP BY s.student_id \n"
. "ORDER BY s.last_name ASC;";

$result = mysqli_query($connection, $sql) or die("Bad Query: $sql"); 

echo"<table>";
echo"<tr>
        <td><b>Student Name</b></td>
        <td><b>Total Absent</b></td>
        <td><b>Dates Absent</b></td>
    </tr>";
$result = mysqli_query($connection, $sql);
$row = mysqli_fetch_assoc($result);
while($row = mysqli_fetch_assoc($result)){
    echo"<tr><td>{$row['StudentName']}</td><td>{$row['TotalAbsent']}</td><td>{$row['DatesAbsent']}</td></tr>";
}

我不知道数据在哪里丢失了。数据将流向何方?有没有办法检查它是来自查询还是表有问题?
在我的第二个表中,我有下面的代码,它也会产生丢失的结果。

<?php include 'includes/header.php';?>
<html>
<body>
<?php
    //SELECT s.first_name,s.last_name,g.grade, a.assignment_name FROM `grades` g JOIN `students` s ON s.student_id = g.student_id JOIN `assignments` a ON a.assignment_id=g.assignment_id

    $servername = "localhost";
    $usernameDB = "root";
    $passwordDB = "";
    $nameDB = "teacheasy";
    //connect to the database
    $connection = new mysqli($servername, $usernameDB, $passwordDB, $nameDB);

    //this section creates the column headers
    $sqlAssignment = "SELECT DISTINCT assignment_name FROM assignments WHERE teacher_id='" . $_SESSION['userId'] . "' AND subject_id = '2';";
    $resultAssignment = mysqli_query($connection, $sqlAssignment) or die("Bad Query: $sqlAssignment"); 
    echo"<table>";
    $resultAssignemnt = mysqli_query($connection, $sqlAssignment);

    echo "<tr>";
    echo "<td><b>Students</b></td>";
    while($row = mysqli_fetch_assoc($resultAssignment)){
        echo"<td><b>{$row['assignment_name']}</b></td>";
    }
    echo "</tr>";

    $sql = "SELECT CONCAT(s.last_name, ', ',s.first_name) AS 'Student Name',g.grade FROM `grades` g JOIN assignments a ON a.assignment_id=g.assignment_id JOIN teacher t ON t.teacher_id=a.teacher_id JOIN students s ON s.student_id=g.student_id WHERE a.teacher_id='" . $_SESSION['userId'] . "' AND a.subject_id='2' ORDER BY s.last_name ASC";
    $sqlCount = "SELECT DISTINCT COUNT(assignment_name) FROM `assignments` WHERE subject_id = '2' AND teacher_id= '" . $_SESSION['userId'] . "';";

    $result = mysqli_query($connection, $sql) or die("Bad Query: $sql"); 

    //gets the number of assignments so it can display the table
    $resultCount = mysqli_query($connection, $sqlCount);
    $countNum = $resultCount->fetch_assoc();
    $counter = 0;

    //this loops through the data and outputs it to the table
    echo "<tr>";
    while($row2 = mysqli_fetch_assoc($result)){
        if($counter == 0){
            echo"<td>{$row2['Student Name']}</td>";
            $counter++;
        } else if($counter <= $countNum['COUNT(assignment_name)']){
            echo"<td>{$row2['grade']}</td>";
            $counter++;
        } else{ 
            $counter = 0;
            echo"</tr>";
        }
    }
    echo "</table>";
?>

</body>

表中的结果是:

phpmyadmin结果如下:

bnl4lu3b

bnl4lu3b1#

获取一行,然后在下一行中再次获取,开始while循环。删除第一个回迁。

$result = mysqli_query($connection, $sql);
while($row = mysqli_fetch_assoc($result)){
    echo"<tr><td>{$row['StudentName']}</td><td>{$row['TotalAbsent']}</td><td>{$row['DatesAbsent']}</td></tr>";
}

相关问题