title: PHP PDO lastInsertId() returns 0 after inserting into table, No Errors or Warnings
body: <p>I am trying to get the ID # that is inserted into a MYSQL database after a record is inserted (using auto-increment).  I am using PHP PDO lastInsertId().  I'm using PHP version 7.4 and MySQL version 8.0.16.</p>
  <p>Here are my classes and code. I have removed some of the column names and replaced with &quot;...&quot; for brevity.</p>
  <pre><code>class DB {
  
  private $servername;
  private $username;
  private $password;
  private $dbname;
  
  protected function connect() {
      $this-&gt;servername =&quot;removed&quot;;
      $this-&gt;username =&quot;removed&quot;;
      $this-&gt;password =&quot;removed&quot;;
      $this-&gt;dbname =&quot;removed&quot;;
  
      
      try {
          $dsn = &quot;mysql:host=&quot;.$this-&gt;servername.&quot;;dbname=&quot;.$this-&gt;dbname; //Data Source Name
          $pdo = new PDO($dsn, $this-&gt;username, $this-&gt;password);
          //$pdo-&gt;setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
          $pdo-&gt;setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
          $pdo-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
          return $pdo;
      } catch(\Exception $exception) {
          echo(&quot;Connection Failed: &quot;.$exception-&gt;getMessage());
          die();
      }
   
  
     }
  }
  
      class Product extends DB
      {
      public function addProduct(int $CategoryID, int $BrandID....)
          {        
                  $sql = &quot;INSERT into products (CategoryID, BrandID, ....) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)&quot;;
                  $stmt = $this-&gt;connect()-&gt;prepare($sql);
                  $stmt-&gt;execute([$CategoryID, $BrandID, .....]);
                  $ProductID = $this-&gt;connect()-&gt;lastInsertId();            
          }
  }
  </code></pre>
  <p>The code inserts a record into the DB, assigns a unique identifier, but then returns a 0 for $ProductID.  I checked my logs and there are no errors or warnings from PHP. Any idea what I am doing wrong here?</p>