簡體   English   中英

我如何在php中顯示所有行

[英]How I can display all rows in 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中有三行時,只顯示第一行。 在此處輸入圖片說明

您需要重復調用fetch_*才能從結果集中檢索所有行; 每次調用它都會檢索結果集中的下一行。

在上面的示例代碼中,您將替換

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

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

這將一直循環直到fetch_array嘗試讀取$result的最后一條記錄以外的內容,此時fetch_array返回false並退出循環。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM