php - 如何在PHP代码中从数据库的第一行检索数据

标签 php html sql

我编写这段代码是为了从数据库中检索一些数据,但是代码不显示表中从第二行开始的第一个值。 我如何使这段代码从第一行检索数据。

<html>
<head>
<title>hello</title>
</head>
<body>
<?php
$con = mysqli_connect('localhost', 'root', '');
mysqli_select_db($con,"uoh");  
$q = " SELECT * FROM student_record WHERE id =201102887;";
$result = mysqli_query($con , $q ) ;
if($row = mysqli_fetch_array($result)) {
    echo "<table border=\"1\" style=\"width:500\">";
    echo "<tr>";
    echo "<th>courses</th>";
    echo "</tr>";
    while($row = mysqli_fetch_array($result))
    {
       echo "<tr>";
       echo "<td>" . $row["grade"]. "</td>";
       echo "</tr>";
    }
    echo "</table>";
}   
?>
</body>
</html>

最佳答案

改变

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

if(mysqli_num_rows($result) > 0) {

更新代码

<html>
<head>
<title>hello</title>
</head>
<body>
<?php
$con = mysqli_connect('localhost', 'root', '');
mysqli_select_db($con,"uoh");  
$q = " SELECT * FROM student_record WHERE id =201102887;";
$result = mysqli_query($con , $q ) ;
if(mysqli_num_rows($result)>0) {
    echo "<table border=\"1\" style=\"width:500\">";
          echo "<tr>";
            echo "<th>courses</th>";
          echo "</tr>";
          while($row = mysqli_fetch_array($result))
          {
             echo "<tr>";
                echo "<td>" . $row["grade"]. "</td>";
             echo "</tr>";
          }
    echo "</table>";
}   
?>
</body>
</html>

关于php - 如何在PHP代码中从数据库的第一行检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36098853/

相关文章:

php - 如何有效地同时哈希多个密码?

php - if 内的 SQL 列数据

php - 正则表达式替换中的换行符

php - Swift:如何查询外部数据库执行登录?

javascript - HTML5 中是否可以有多个 data-{name} 属性?

html - 如果 <div> 的内容不适合宽度,则水平滚动条

html - 如何更改 :hover color from my social bar?

sql - 将字符串转换为具有非标准值的日期时间格式

SQL Split Island On Criteria

python - (Python) 编辑 SQL 数据库中的表?