0

我有 2 个不同的数据库,它们与出现在两个表中的主键“id”链接。

数据库中不同的 ID 有不同的记录。

我有一个按钮,当单击从两个数据库表中提取所有内容时,它们都出现在一个表中,所有记录在 td 单元格中相互堆叠。

像这样的东西:

Id----name----address---grade1----grade2---grade3---
Id----name----address---grade1----grade2---grade3---
Id----name----address---grade1----grade2---grade3---
Id----name----address---grade1----grade2---grade3---
Id----name----address---grade1----grade2---grade3---
Id----name----address---grade1----grade2---grade3---

我希望他们以以下格式打印出来:

ID - xxxxxx
name - xxxxxxx
address - xxxxxxx

与 id 关联的成绩表(多行,4 列)

ID - xxxxxx
name - xxxxxxx
address - xxxxxxx

与 id 关联的成绩表(多行,4 列)

ID - xxxxxx
name - xxxxxxx
address - xxxxxxx

与 id 关联的成绩表(多行,4 列

这是PHP代码

<style> table, td, caption { border: 2px solid black ; padding: 10px; } </style>
<style> td { border: 1px solid green ; width: 100px;} </style>
<style> caption { color: green ; } </style>

<?php
include ("../../php/account.php");
$dbh = mysql_connect ( $hostname, $username, $password )
                   or die ( "Unable to connect to MySQL database" );
mysql_select_db( $project );

$s="SELECT * from newstudent, grades where newstudent.id=grades.id";  
($t=mysql_query($s)) or die('Error! Student not found!');

print "<table>";
print "<caption>"; 
print "Grade report for all the Registered Students";
print "</caption>";
print "<tr> <th>Student ID</th> <th>First Name</th> <th>Last Name</th> <th>Email</th> <th>Course ID</th> <th>Grade One</th> <th>Grade Two</th> <th>Grade Three</th> <th>Total</th>";
//2. Get rows $r
while ($r = mysql_fetch_array($t))
{
//3. Get columns of row $r["---"]
 print "<tr>";
 print "<td>";
 print $r["id"];
 print "</td>";

    print "<td>";
    print $r["firstname"];
    print "</td>";

    print "<td>";
    print $r["lastname"];
    print "</td>";

print "<td>";
print $r["email"];
print "</td>";



 print "<td>";
 print $r["subject"];
 print "</td>";

    print "<td>";
    print $r["gradeone"];
    print "</td>";

print "<td>";
print $r["gradetwo"];
print "</td>";

    print "<td>";
    print $r["gradethree"];
    print "</td>";

    print "<td>";
    print $r["Total"];
    print "</td>";

}
print "</table>";
4

1 回答 1

0

我想你想要一个这样的 sql 查询

SELECT * from newstudent as s inner join grades as g on s.id=g.id

但是当我查看你的代码时,我认为你必须更改你的成绩数据库,我猜你会有很多空值我会制作这样的成绩表

id - student_id - 年级

并将它们链接到 student_id

于 2013-10-31T19:49:21.967 回答