title: Using different mysql users within same class while maintaining same connection?
body: <p>I'm in a situation where I need to use MYSQL's <code>LAST_INSERT_ID()</code>, which is obviously only possible when maintaining the same connection. But as I was thinking about the way I establish my PDO connections, it occurred to me that I am probably going to run into problems.</p>
  
  <p>I've got a database class with various types of connections. Inside this class, I've created different PDO connection methods based on user permissions which I call inside the PDO query methods like so...</p>
  
  <pre><code>class dbFunctions {
  
      private $userSelect = "userSelect";
      private $passSelect = "XXXXX";
      private $userDelete = "userDelete";
      private $passDelete = "XXXXX";
  
      protected function connectSelect() {
          $dsn = $this-&gt;dsn();
          $this-&gt;pdo = new PDO($dsn, $this-&gt;userSelect, $this-&gt;passSelect, $this-&gt;options);
  
          return $this-&gt;pdo;
      }
  
      protected function connectDelete() {
          $dsn = $this-&gt;dsn();
          $this-&gt;pdo = new PDO($dsn, $this-&gt;userDelete, $this-&gt;passDelete, $this-&gt;options);
  
          return $this-&gt;pdo;
      }
  
      public function selectCount($query, $values = []) {
  
          $result = $this-&gt;connectSelect()-&gt;prepare($query);
          $result-&gt;execute($values);
          $exec = $result-&gt;fetch();
  
          $count = (int)$exec['total'];
  
          return $count;
      }
  }
  </code></pre>
  
  <p>My questions are...  </p>
  
  <ul>
  <li>Am I better off creating one global connection object with the same account/password for all queries? I did it this way for security purposes but I believe it may have drawbacks.</li>
  <li>Even though these are different PDO connection objects, is PHP able to determine they are part of the same session within the same script? In which case the separate methods will be ok.</li>
  <li>How can I keep separate mysql users with my current class?</li>
  </ul>
  
  <p>Obviously I can test this on my own, but I'm unsure what the results will be when I get multiple users accessing the site.</p>