php mysql I need to read one record at a time until the end

advertisements

I have read several post stack overflow and I wrote this small program. However, instead of reading a record at a time read the same record ... where am I wrong?

// ...................................... 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>";
}


To specify which record to retrieve, you need to specify the offset and number of record to retrieve. See this MySQL reference

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

From the code you posted, it seems that you want to retrieve all the data from tsynapse. This is a better approach which simplifies your code:

$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']  ;
}