2016-03-13 27 views
0

顯示所有行寫了這個代碼來檢索某些行形成數據庫我我如何在PHP

session_start(); 
$con = mysqli_connect('localhost', 'root', ''); 
if(!$con) 
{ 
    die("not ok"); 
} 

mysqli_select_db($con,"uoh"); 

$q = " SELECT * FROM student WHERE id = " . $_SESSION['user_id'] ." and password = " . $_SESSION['user_pass']; 
$result = mysqli_query($con , $q) ; 
if($row = mysqli_fetch_array($result)) 
{ 
    echo "this academic transcripts for " . $row["name"]; 
    echo " and the id is " . $row["id"]; 

} 

$q1 = " SELECT student_record.course,student_record.grade,student_record.term,coe_courses.crd 
    FROM student_record INNER JOIN coe_courses ON student_record.course_number = coe_courses.course_number 
    where student_record.id = ".$_SESSION['user_id'] ; 
$result = mysqli_query($con , $q1) ; 
if($row = mysqli_fetch_array($result)) 
{ 
    echo "<br />"; 
    echo "<table border=\"1\" style=\"width:500\">"; 
    echo "<tr>"; 
    echo "<th>coe_courses</th>"; 
    echo "<th>terms</th>"; 
    echo "<th>Grades</th>"; 
    echo "<th>CRD</th>"; 
    echo "</tr>"; 
    echo "<tr>"; 
    echo "<td>" . $row["course"]. "</td>"; 
    echo "<td>" . $row["term"]. "</td>"; 
    echo "<td>" . $row["grade"]. "</td>"; 
    echo "<td>" . $row["crd"]. "</td>"; 
    echo "</tr>"; 
    echo "</table>"; 
} 

的問題是,只顯示第一行,而我在phpMyAdmin三行。 enter image description here

回答

2

你需要調用fetch_*反覆來檢索結果集中的所有行;每次您調用它時,都會檢索結果集中的下一個行。

在上面的示例代碼,你將取代

if ($row = mysqli_fetch_array($result)) 
{ 

while ($row = mysqli_fetch_array($result)) 
{ 

這將循環播放,直到fetch_array試圖讀取超出$result的最後一個記錄,在這一點fetch_array回報false和循環退出。