title: Get the id of a newly inserted row php
body: <p>I am trying to get the insert id from a newly inserted row. I use the following method to query. However I am unable to get the inserted id.</p>
  
  <pre><code>function query($sql) {
      global $link;
          $debug = false;
  
      //get the sql query
      $args = func_get_args();
      $sql = array_shift($args);
  
      //secure the input
      for ($i=0;$i&lt;count($args);$i++) {
          $args[$i] = urldecode($args[$i]);
          $args[$i] = mysqli_real_escape_string($link, $args[$i]);
      }
  
      //build the final query
      $sql = vsprintf($sql, $args);
  
      if ($debug) print $sql;
  
      //execute and fetch the results
      $result = mysqli_query($link, $sql);
  
          $id = $result-&gt;insert_id; //no result, what do I put here?
  
      if (mysqli_errno($link)==0 &amp;&amp; $result) {
  
          $rows = array();
  
          if ($result!==true)
          while ($d = mysqli_fetch_assoc($result)) {
               array_push($rows,$d);
          }
          //return json
          return array('result'=&gt;$rows, 'id'=&gt;$id);
  
      } else {
  
          //error
          return array('error'=&gt;'Database error');
      }
  }
  </code></pre>
  
  <p>How can I modify this method to get the insert id?</p>