title: getting lastInsertId from PDO class
body: <p>I'm using this class to connect to database. It works just fine, except I couldn't get the <strong>lastInsertId()</strong>.         </p>
  
  <pre><code>&lt;?php
  class connDB
  { 
  
  public function connDB()
     {
  
      require_once( 'dbconfig/config.php' );
  
      $this-&gt;confPDO = array(
                            PDO::ATTR_ERRMODE =&gt; PDO::ERRMODE_EXCEPTION,  
                            PDO::ATTR_PERSISTENT =&gt; false, 
  
                PDO::MYSQL_ATTR_INIT_COMMAND =&gt; "SET NAMES UTF8"
                           );
  
      try
       {
       $this-&gt;dbc = new PDO( "mysql:host=$this-&gt;dbHost;dbname=$this-&gt;dbName", 
       $this-&gt;dbUser, $this-&gt;dbPass, $this-&gt;confPDO );
       }
  
     catch( PDOException $errMsg )
       {
        return false;
       }
  
     }
  
     public function exec( $sql, array $params = array() )
     {
  
     try
       {
       $this-&gt;stmt = $this-&gt;dbc-&gt;prepare( $sql );
        if ( count( $params ) &gt; 0 )
          {
          foreach ( $params as $k=&gt;$v )
                  {
                  $this-&gt;bind($k, $v);
                  }
          }
          return $this-&gt;stmt-&gt;execute();
       }
  
  
     catch( PDOException $errMsg )
       {
  
       $this-&gt;dbc = null;
  
       return false;
       }
  
     }
  
  
    public function bind( $param, $value, $type = null )
     {
     if ( is_null( $type ) )
        {
        switch ( true )
               {
               // Boolen parameter
               case is_bool( $value ):
                    $type = PDO::PARAM_BOOL;
                    break;
               // Integer parameter
               case is_int( $value ):
                    $type = PDO::PARAM_INT;
                    break;
               // Null parameter
               case is_null( $value ):
                    $type = PDO::PARAM_NULL;
                    break;
               // String parameter
               default:
                    $type = PDO::PARAM_STR;
               }
        }
        $this-&gt;stmt-&gt;bindValue( $param, $value, $type );
     }
  
  
  
    public function single()
     {
  
     return $this-&gt;stmt-&gt;fetch(PDO::FETCH_ASSOC);
     }
  
  
    public function resultset()
     {
  
     return $this-&gt;stmt-&gt;fetchAll(PDO::FETCH_ASSOC);
     }
  
  
    public function rowCount()
     {
  
     return $this-&gt;stmt-&gt;rowCount();
     }
  
  
   } 
  </code></pre>
  
  <p><strong>Usage:  [SELECT]</strong></p>
  
  <hr>
  
  <pre><code> $sql = "SELECT * FROM &lt; table &gt;";
   $db-&gt;exec($sql, $params);
   $rows = $db-&gt;resultset();
   foreach ($rows as $row)
         {
         echo $row['&lt; column &gt;'] . "\n";
         }
  </code></pre>
  
  <p><strong>Usage:  [INSERT]</strong></p>
  
  <hr>
  
  <pre><code> $sql = "INSERT INTO &lt; table &gt; (&lt; column_1 &gt;, &lt; column_2 &gt;, ... ) VALUES 
   (:valuename_1, 
   :valuename_2, ...)";
   $params = array(':valuename_1' =&gt; 'value', ':valuename_2' =&gt; 'value', ...);
   $db-&gt;exec($sql, $params);
  </code></pre>
  
  <p>I tried to do it this way:</p>
  
  <pre><code>include_once'classe.php';
  
   $db = new connDB();
  
   $sql = "INSERT INTO &lt; table &gt; (&lt; column_1 &gt;, &lt; column_2 &gt;, ... ) VALUES 
   (:valuename_1, 
   :valuename_2, ...)";
   $params = array(':valuename_1' =&gt; 'value', ':valuename_2' =&gt; 'value', ...);
  
   $db-&gt;exec($sql, $params);
  
   $id = $db-&gt;lastInsertId();
  </code></pre>
  
  <p>I am getting an error:</p>
  
  <pre><code>  Fatal error: Call to undefined method connDB::lastInsertId() in  
  </code></pre>
  
  <p>I've tried adding a method into the class:</p>
  
  <pre><code>     public function lastinsert()
     {
     // Return result
     return $this-&gt;stmt-&gt;lastInsertId();
     }
  </code></pre>
  
  <p>Then I called it like this this:</p>
  
  <pre><code>    $db = new connDB(); 
  $id = $db-&gt;lastinsert();
  </code></pre>
  
  <p>The error this time was</p>
  
  <pre><code> Fatal error: Call to undefined method PDOStatement::lastInsertId() in 
  </code></pre>