简体   繁体   中英

How I can display all rows in php

I wrote this code to retrieve some rows form database

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>";
}

The problem is that only shows the first row while I have three rows in phpMyAdmin. enter image description here

You need to call fetch_* repeatedly to retrieve all rows from your result set; each time you call it it retrieves the next row in the result set.

In your sample code above, you would replace

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

with

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

This will loop until fetch_array tries to read beyond the last record in $result , at which point fetch_array returns false and the loop exits.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM