title: How to get last inserted inserted row id from PDO
body: <p>I am following mvc structure in PHP and I want to retrieve last inserted row ID.</p>
  
  <p><strong>I have created following sql code:</strong></p>
  
  <pre><code>$sql = "INSERT INTO song (artist, track, link) VALUES (:artist, :track, :link)";
  $query = $this-&gt;db-&gt;prepare($sql);
  $query-&gt;execute(array(':artist' =&gt; $artist, ':track' =&gt; $track, ':link' =&gt; $link));
  
  echo $query-&gt;lastInsertId(); // To retrieve last inserted row ID.
  </code></pre>
  
  <p>but unfortunately I ma getting this error: <code>Fatal error: Call to undefined method PDOStatement::lastInsertId()</code> </p>
  
  <p>I have also tried this <a href="https://stackoverflow.com/q/12970626/3113858">stack</a> links but not worked for me so I will happy if you help me for retrieve ID.</p>
  
  <p>I am also sharing my controller.php file here.</p>
  
  <p>
  
  <pre><code>/**
   * This is the "base controller class". All other "real" controllers extend this class.
   */
  class Controller{
      /**
       * @var null Database Connection
       */
      public $db = null;
  
      /**
       * Whenever a controller is created, open a database connection too. The idea behind is to have ONE connection
       * that can be used by multiple models (there are frameworks that open one connection per model).
       */
      function __construct(){
          $this-&gt;openDatabaseConnection();
      }
  
      /**
       * Open the database connection with the credentials from application/config/config.php
       */
      private function openDatabaseConnection(){
          // set the (optional) options of the PDO connection. in this case, we set the fetch mode to
          // "objects", which means all results will be objects, like this: $result-&gt;user_name !
          // For example, fetch mode FETCH_ASSOC would return results like this: $result["user_name] !
          // @see http://www.php.net/manual/en/pdostatement.fetch.php
          $options = array(PDO::ATTR_DEFAULT_FETCH_MODE =&gt; PDO::FETCH_OBJ, PDO::ATTR_ERRMODE =&gt; PDO::ERRMODE_WARNING);
  
          // generate a database connection, using the PDO connector
          // @see http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/
          $this-&gt;db = new PDO(DB_TYPE . ':host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USER, DB_PASS, $options);
      }
  
      /**
       * Load the model with the given name.
       * loadModel("SongModel") would include models/songmodel.php and create the object in the controller, like this:
       * $songs_model = $this-&gt;loadModel('SongsModel');
       * Note that the model class name is written in "CamelCase", the model's filename is the same in lowercase letters
       * @param string $model_name The name of the model
       * @return object model
       */
      public function loadModel($model_name){
          require 'application/models/' . strtolower($model_name) . '.php';
          // return new model (and pass the database connection to the model)
          return new $model_name($this-&gt;db);
      }
  }
  </code></pre>