duannian4784 2016-03-19 06:58
浏览 63
已采纳

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

I wrote this code to retrieve some data from data base But the code do not display the first value in the table which is started from second row. How I can make this code retrieve the data from first row.

<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>
  • 写回答

3条回答 默认 最新

  • donglong7338 2016-03-19 07:03
    关注

    Change

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

    to

    if(mysqli_num_rows($result) > 0) {
    

    Updated Code

    <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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法