How to get the last record from my table?

I am calling some data from my table in phpmyadmin and it duplicates the table for the amount of data that is in the one of phpmyadmin, and I only want the last data that is in my database

    </div>
      <?php

    $sql="SELECT * from roberto";
    $result=mysqli_query($conexion,$sql);

    while($mostrar=mysqli_fetch_array($result)){
    ?>
     <table class="table is-striped is-narrow is-hoverable is-fullwidth">
  <thead>
    <tr>
      <th>Tipo</th>
      <th>Niveles</th>
      <th>Estado</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>pH Sensor <spab></th>
      <td><p>Nivel de pH: <strong><span id="PHvalue"></span><?php echo $mostrar['nivelph']; ?></strong></p>  
      </td>
      <td>Funcionando</td>
  </tbody>
  <tbody>
    <tr>
      <th>Caudalímetro</th>
      <td><p><strong><?php echo $mostrar['caudal']; ?>10L/min</strong></p>
      </td>
      <td>Funcionando</td>
  </tbody>
  <tbody>
    <tr>
      <th>Bomba subir pH ↑</th>
      <td><span>""" + str(ph) + """pH</span>
      </td>
      <td>Subiendo el pH↑</td>
  </tbody>
  <tbody>
    <tr>
      <th>Bomba bajar pH ↓</th>
      <td><span>""" + str(ph) + """pH</span>
      </td>
      <td>Bajando el pH↓</td>
  </tbody>
    <tr>
      <th>Nutrientes</th>
      <td><span>1mL/24horas</span>
      </td>
      <td>Activando bomba</td>
  </tbody>
</table>
</section>
<?php
}
?>

ㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤ

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

If you want the last of all the rows in the table, then this is answer:

SELECT * FROM roberto ORDER BY id DESC LIMIT 1;

Method 2

SELECT * FROM table_name ORDER BY id DESC LIMIT 1;

Method 3

this code brings the last id inserted in the database connection

printf("Last inserted record has id %dn", mysql_insert_id($conn));


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x