2016-04-14 44 views
0

我试图打印多个表中的数据到一个HTML表格,但我似乎无法找出原因无非是表格标题都出现了。为什么我的mySQL表数据不会回显?

<?php 

include('includes/db_connect.php'); 

$query_student="SELECT student.firstName, student.lastName, major.major, major.gradDate, 
       FROM student 
       JOIN major 
       ON student.studentID=major.studentID"; 
    $result_student=mysqli_query($conn, $query_student); 

echo '<table> 
    <tr> 
     <th>First Name</th> 
     <th>Last Name</th> 
     <th>Graduate Year</th> 
     <th>Major</th> 
     <th>Activity After Graduation</th> 
    </tr>'; 

while($row = mysqli_fetch_array($result_student)) 
{ 
    echo'<tr>'; // printing table row 
     echo '<td>' . $row['firstName'] . '</td>'; 
     echo '<td>' . $row['lastName'] . '</td>'; 
     echo '<td>' . $row['gradDate'] . '</td>'; 
     echo '<td>' . $row['major'] . '</td>'; 
    echo'</tr>'; // closing table row 
} 
echo '</table>'; 

$conn->close(); 
?> 
+0

你在其他环境运行查询,像phpMyAdmin,看它是否返回结果? –

+0

运行查询后添加错误检查 – Jens

回答

0

开始从查询移除,major.gradDate,后,你有没有其他字段跟随。

你真的应该在你的查询中检查错误知道发生了什么......

变化:$result_student=mysqli_query($conn, $query_student);

要:

$result_student=mysqli_query($conn, $query_student) or die(mysqli_error($conn));

0

您可以使用foreach实现这一目标声明和mysqli对象的query方法。

我也修正了自己的SQL,你把两个表在一起的时候需要LEFT JOIN。请首先检查此SQL语句是否适用于环境,因为它尚未经过测试。

$query_student="SELECT t1.firstName, t1.lastName, t2.major, t2.gradDate 
       FROM student t1 
       LEFT JOIN SECOND_TABLE t2 
       ON t1.studentID = t2.studentID"; 

// Replace SECOND_TABLE with your table name you're joining on 

?> <tr> 
<?php 
    if($conn): 
     foreach ($conn->query($query_student) as $row): ?> 
     <!-- conditional statements allow HTML inside PHP loops --> 
     <td> <?php echo $row['firstName']; ?> </td> 
     // [...] 
    <?php endforeach; 
    else: 
     die('Connection Error: ' . $conn->error); 
    endif; ?> 
</tr> 

希望这对我有所帮助。

相关问题