从mysql数据库中检索记录

时间:2013-10-31 19:44:33

标签: php html mysql

我有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>";

1 个答案:

答案 0 :(得分:0)

我想你想要像这样的SQL查询

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

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

id - student_id - 年级

并将它们链接到student_id