title: MySQLi query to get maximal id in the table?
body: <pre><code> $mysqli = new mysqli("localhost","root","","mydatabase");
  
   if ($result = $mysqli-&gt;prepare("SELECT MAX(`id`) AS `id` FROM `mytable` WHERE `row1`=? OR `row2`=?"))
   {
  
      $id = 2;
      $result-&gt;bind_param("ii",$id,$id);
      $result-&gt;execute();
      $result-&gt;bind_result($max);
      $result-&gt;close();
  
      var_dump($max);
  
   }
  
   $mysqli-&gt;close();
  </code></pre>
  
  <p>Unfortunately this code always showing <strong>NULL</strong>, can u folk explain me how to reach a result?</p>
  
  <p><strong>updated:</strong></p>
  
  <p>in console mode staff like this works great. field <code>id</code> is int and incremental (as PRIMARY INDEX), other fields it's just a rows with a different int values, I cant change anything.</p>
  
  <p><strong>updated:</strong></p>
  
  <p>Well, seems I found the solution:</p>
  
  <pre><code> $mysqli = new mysqli("localhost","root","","mydatabase");
  
   if ($result = $mysqli-&gt;prepare("SELECT MAX(`id`) AS `id` FROM `mytable` WHERE `row1`=? OR `row2`=?"))
   {
  
      $id = 2;
      $result-&gt;bind_param("ii",$id,$id);
      $result-&gt;execute();
      $obj = $result-&gt;get_result()-&gt;fetch_object();
      $max = $obj-&gt;id;
      $result-&gt;close();
  
      var_dump($max);
  
   }
  
   $mysqli-&gt;close();
  </code></pre>
  
  <p>this is it.</p>