2013-10-31 262 views
0

我有兩個不同的數據庫,它們與出現在兩個表中的「id」主鍵鏈接。從mysql數據庫中檢索記錄

數據庫中不同的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關聯等級
ID - xxxxxx 
name - xxxxxxx 
address - xxxxxxx 

表(多行,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>"; 
+2

將它們鏈接請_DO沒有USE_老和過時的'mysql_'功能。改爲使用'mysqli_''或PDO。 – akluth

回答

0

我想你想的SQL查詢這樣

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

但是當我看着你的代碼我認爲你必須改變你的成績分數我想你會有很多空值 我會做的等級表這樣

ID - student_id數據 - 級

和student_id數據

相關問題