【问题标题】:How to retrieve data from the first row from database in php code如何在php代码中从数据库的第一行检索数据
【发布时间】:2016-03-19 06:58:41
【问题描述】:

我编写了这段代码来从数据库中检索一些数据但是代码不显示从第二行开始的表中的第一个值。 我怎样才能让这段代码从第一行检索数据。

<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>

【问题讨论】:

  • But the code do not display the first value in the table which is started from second row.?无法理解
  • 从缺少表格第一行的第二行开始检索。

标签: php html sql


【解决方案1】:

改变

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>

【讨论】:

    【解决方案2】:

    据我所知,当您调用两次时,

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

    那么当你在循环中再次调用它时第一行会跳过,你创建 if($row = mysqli_fetch_array($result)) { 的点是什么?如果您只想检查查询返回,您可以使用if($result) {

    【讨论】:

      【解决方案3】:

      您调用 mysqli_fetch_array 两次第二次调用将转到第二行。试试这样

      <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 ) ;
        $rows = array();
        while($row = mysqli_fetch_array($result))
        {
          $rows[] = $row;
        }
      
        if(count($rows) > 0) {
            echo "<table border=\"1\" style=\"width:500\">";
            echo "<tr>";
            echo "<th>courses</th>";
            echo "</tr>";
            foreach($rows as $row)
            {
               echo "<tr>";
               echo "<td>" . $row["grade"]. "</td>";
               echo "</tr>";
            }
            echo "</table>";
        }   
      ?>
      </body>
      </html>
      

      【讨论】:

      • 为什么?任何评论
      猜你喜欢
      • 1970-01-01
      • 2013-09-06
      • 2018-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-10
      相关资源
      最近更新 更多