title: How can I return LastInsertID from PDO whin a method of a class
body: <p>I have create a small class that handles my MySql queries.</p>
  
  <p>However, Now I am trying to get the value of the last inserted id but it is not wokring for me</p>
  
  <p>if my processQuery method in the class I do a prepare statement with any query like insert/update/remove</p>
  
  <p>I have added this line $this->lastInsertId = $this->pdo->lastInsertId; which should give me the last inserted Id and stores it in the public variable called $lastInsertId then I can access it from my code outside.</p>
  
  <p>How can I get the last inserted ID to work with this class??</p>
  
  <p>Thanks</p>
  
  <p>this is my class</p>
  
  <pre><code>&lt;?php
  
  class connection {
  
      private $connString;
      private $userName;
      private $passCode;
      private $server;
      private $pdo;
      private $errorMessage;
      public $lastInsertId;
  
      private $pdo_opt = array (
                              PDO::ATTR_ERRMODE            =&gt; PDO::ERRMODE_EXCEPTION,
                              PDO::ATTR_DEFAULT_FETCH_MODE =&gt; PDO::FETCH_ASSOC
                              );
  
  
      function __construct($dbName, $serverName = 'localhost'){
  
          //sets credentials
          $this-&gt;setConnectionCredentials($dbName, $serverName);
  
          //start the connect
          $this-&gt;startConnection();
  
      }
  
      function startConnection(){
  
  
              $this-&gt;pdo = new PDO($this-&gt;connString, $this-&gt;userName, $this-&gt;passCode, $this-&gt;pdo_opt);
  
              if( ! $this-&gt;pdo){
  
                  $this-&gt;errorMessage  = 'Failed to connect to database. Please try to refresh this page in 1 minute. ';
                  $this-&gt;errorMessage .= 'However, if you continue to see this message please contact your system administrator.';
  
  
              }
  
      }
  
  
      //this will close the PDO connection
      public function endConnection(){
  
          $this-&gt;pdo = null;
      }
  
      //return a dataset with the results
      public function getDataSet($query, $data = NULL)
      {
          $cmd = $this-&gt;pdo-&gt;prepare( $query );
  
          $cmd-&gt;execute($data);
  
          return $cmd-&gt;fetchAll();
      }
  
  
      //return a dataset with the results
      public function processQuery($query, $data = NULL)
      {
          $cmd = $this-&gt;pdo-&gt;prepare( $query );
          $this-&gt;lastInsertId = $this-&gt;pdo-&gt;lastInsertId;
          return $cmd-&gt;execute($data);
      }
  
  
      //this where you need to set new server credentials with a new case statment
      function setConnectionCredentials($dbName, $serv){
  
          switch($serv){
  
              case 'BLAH':
                  $this-&gt;connString   = 'mysql:host='.$serv.';dbname='.$dbName.';charset=utf8';
                  $this-&gt;userName     = 'BLAH';
                  $this-&gt;passCode     = 'BLAH';
              break;
  
              default:
                  $this-&gt;connString   = 'mysql:host='.$serv.';dbname='.$dbName.';charset=utf8';
                  $this-&gt;userName     = 'BLAH2';
                  $this-&gt;passCode     = 'BLAH2';
              break;
  
              }
  
      }
  
  }
  
  ?&gt;
  </code></pre>