2

我已经阅读了几篇堆栈溢出后,并编写了这个小程序。但是,不是一次读取一条记录,而是读取同一条记录……我哪里错了?

// ...................................... NUMBER OF RECORD
$result = mysqli_query($data->connessione,"SELECT COUNT(id) FROM tsynapse")  ;
if ( $result == false ) { printf ("<br/> mysqli : Error Reading FROM tsynapse; <br/>");  break ; }
echo "<br/> record totali <br/>" ;
$row = mysqli_fetch_array($result) ;
echo $row[0] . "<br/>";

// ...................................... COUNTER
$k=$row[0] ;

while ( $k-- )
{
    // ...................................... READ ONE RECORD AT TIME
    $result = mysqli_query($data->connessione,"SELECT * FROM tsynapse LIMIT 1")  ;
    if ( $result == false ) { printf ("<br/> mysqli : Error Reading FROM tsynapse; <br/>");  break ; }

      $row = mysqli_fetch_array($result);
      echo $row['id'] . " XXX " . $row['tag'] . " XXX " . $row['token'] . " XXX " . $row['rule']. " XXX " . $row['trans']  ;
      echo "<br>";
}
4

3 回答 3

2

要指定要检索的记录,您需要指定要检索的记录的偏移量和数量。请参阅此 MySQL 参考

while ($k--)
{
   $result = mysqli_query ($data->connessione, "SELECT * FROM tsynapse LIMIT {$k}, 1");
   // etc.
}

从您发布的代码中,您似乎想从中检索所有数据tsynapse。这是一种更好的方法,可以简化您的代码:

$result = mysqli_query ($data->connessione, "SELECT * FROM tsynapse ORDER BY id DESC"); // Since it seems that you want to retrieve from last record.
$total_record_count = mysqli_num_rows ($result);

while ($row = mysqli_fetch_assoc ($result))
{
   echo $row['id'] . " XXX " . $row['tag'] . " XXX " . $row['token'] . " XXX " . $row['rule']. " XXX " . $row['trans']  ;
}
于 2013-08-16T17:43:33.610 回答
1

您为每次迭代执行查询。因此,您每次都会得到相同的结果集,并且每次都选择其中的第一行。

在迭代之前只执行一次查询,然后一一选择行。

于 2013-08-16T17:37:11.830 回答
1

您需要在循环外执行您的请求并删除limit 1您的查询

// ...................................... NUMBER OF RECORD
$result = mysqli_query($data->connessione,"SELECT COUNT(id) FROM tsynapse")  ;
if ( $result == false ) { printf ("<br/> mysqli : Error Reading FROM tsynapse; <br/>");  break ; }
echo "<br/> record totali <br/>" ;
$row = mysqli_fetch_array($result) ;
echo $row[0] . "<br/>";

// ...................................... COUNTER
$k=$row[0] ;

// ...................................... READ ONE RECORD AT TIME
        $result = mysqli_query($data->connessione,"SELECT * FROM tsynapse")  ;
        if ( $result == false ) { printf ("<br/> mysqli : Error Reading FROM tsynapse; <br/>"); }

while ( $k-- )
{     
      $row = mysqli_fetch_array($result);
      if($row == null)
      {
          break;
      }
      echo $row['id'] . " XXX " . $row['tag'] . " XXX " . $row['token'] . " XXX " . $row['rule']. " XXX " . $row['trans']  ;
      echo "<br>";
    //}
}
于 2013-08-16T17:39:38.240 回答