使用php从数据库中获取所有行

时间:2014-09-28 07:24:16

标签: php mysql

我试图创建一个管理页面,其中管理员可以查看数据库中的所有数据,但我的问题是每当我尝试选择所有数据时它只返回一行,这是第一个数据id为1。

这是我的php代码:

$query_select = "SELECT `users_id`,`users_studno`,`users_fname`,`users_lname`,`users_email`,`users_password` FROM `tbl_usersinfo`";
        if($run_query = mysqli_query($con,$query_select))
        {
            if($row = mysqli_fetch_assoc($run_query))
            {
                $id_val = htmlentities($row['users_id']);
                $studno_val = htmlentities($row['users_studno']);
                $fname_val = htmlentities($row['users_fname']);
                $lname_val = htmlentities($row['users_lname']);
                $email_val = htmlentities($row['users_email']);
                $password_val = htmlentities($row['users_password']);

                echo "<tr>";
                    echo "<td>".$id_val."</td>";
                    echo "<td>".$studno_val."</td>";
                    echo "<td>".$fname_val."</td>";
                    echo "<td>".$lname_val."</td>";
                    echo "<td>".$email_val."</td>";
                    echo "<td>".$password_val."</td>";
                echo "</tr>";
            }
        }

我想输出数据库中的所有数据,但它只返回第一行。

1 个答案:

答案 0 :(得分:2)

您缺少while循环。

试试这个:

$query_select = "SELECT `users_id`,`users_studno`,`users_fname`,`users_lname`,`users_email`,`users_password` FROM `tbl_usersinfo`";
        if($run_query = mysqli_query($con,$query_select))
        {
            while($row = mysqli_fetch_assoc($run_query))
            {
                $id_val = htmlentities($row['users_id']);
                $studno_val = htmlentities($row['users_studno']);
                $fname_val = htmlentities($row['users_fname']);
                $lname_val = htmlentities($row['users_lname']);
                $email_val = htmlentities($row['users_email']);
                $password_val = htmlentities($row['users_password']);

                echo "<tr>";
                    echo "<td>".$id_val."</td>";
                    echo "<td>".$studno_val."</td>";
                    echo "<td>".$fname_val."</td>";
                    echo "<td>".$lname_val."</td>";
                    echo "<td>".$email_val."</td>";
                    echo "<td>".$password_val."</td>";
                echo "</tr>";
            }
        }