php - 如何在 php 中显示所有行

标签 php mysql mysqli

我编写此代码是为了从数据库中检索一些行

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

最佳答案

您需要重复调用fetch_*来检索结果集中的所有行;每次调用它时,它都会检索结果集中的下一行。

在上面的示例代码中,您将替换

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

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

这将循环,直到fetch_array尝试读取$result中最后一条记录,此时fetch_array返回 false 并且循环退出。

关于php - 如何在 php 中显示所有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35974338/

相关文章:

php - 不使用$ _GET 'Undefined Index']格式时如何解决[''错误?

php - 按首字母排序sql查询

mysql - 如果不满足条件,则替换满足条件的行中的值

php - Preg_match 的语法

php - 更新创建简单的 Php、MySql 投票系统的问题

php - 将 wordpress 数据库用于其他脚本

php - 调用 bool 值的成员函数 num_rows()

SQL查询的PHP大量内存使用

php mysql where 子句使用日期

php - PHP file_get_contents 有限制吗?