title: How to get the last id of a MySQL database table, and store it in a variable
body: <p>I am building a website and I want to display user reviews in a Bootstrap carousel. I want to display only the latest 3 feedbacks. The latest review has to have a class "active". In order to do that, I need the last id not just after inserting into the table, but throughout the whole website. I have tried this so far: </p>
  
  <pre><code>$sql = "SELECT MAX(id) FROM comments";
  
  $result = $conn-&gt;query($sql);
  if ($result-&gt;num_rows &gt; 0) {
    while($row = $result-&gt;fetch_assoc()) {
      $last_id = $row["id"];
  }
  </code></pre>
  
  <p>I know there's a way to get the last_id after inserting a new row, but I will always need the last variable. Can anyone help me with this?</p>