title: Query running twice within custom PDO Database class
body: <p>I'm currently attempting to create my own custom database class that will allow me to do basic things such as insert, update, and select from my database. I have the code working for my insert function but it's inserting twice and I'm not sure why. It's been a while since I've messed with the singleton pattern and am having quite a few issues today. Any help would be great. Thanks</p>
  
  <p>The issue method in question here is the <code>insert()</code> method. It uses the <code>db()</code> method to get it's instance of the database connection. </p>
  
  <p>Database.php</p>
  
  <pre><code>class Database {
      // Class Properties
      private static $_instance;
      public $pdo;
  
      /**
       * Returns a database connection
       * @return resource Database Connection
       */
      private static function db() {
          // Return Database Connection
          return Database::getInstance()-&gt;getConnection();
      }
  
      private function getConnectionProperties($property) {
          return $GLOBALS['config']['database'][$property];
      }
  
      /**
       * Singleton for database connection
       * @return class Returns one instance of the Database class
       */
      public static function getInstance() {
          if(!self::$_instance) {
              self::$_instance = new self();
          }
          return self::$_instance;
      }
  
      /**
       * Connects to database
       * @return mysql PDO connection to mysql databse
       */
      public function getConnection() {
          try {
              //Attempt to connect
              $this-&gt;pdo = new PDO('mysql:host=' . $this-&gt;getConnectionProperties('host') . ';dbname=' . $this-&gt;getConnectionProperties('database') , $this-&gt;getConnectionProperties('username'), $this-&gt;getConnectionProperties('password'));
  
              //Return connection
              return $this-&gt;pdo;
          } catch (PDOException $e) {
              throw new PDOException($e);
          }
      }
  
      public static function insert($table, $params) {
          // Define initial query
          $query = "INSERT INTO `{$table}` (";
  
          // Format rows to insert
          foreach(array_keys($params) as $field) {
              $fields .= $field . ",";
              $bindParams .= ":" . $field . ",";
          }
  
          // Add rows and bind params to query
          $query .= rtrim($fields, ',') . ") VALUES(" . rtrim($bindParams, ',') . ")";
  
          // Prepare Query
          $preparedQuery = self::db()-&gt;prepare($query);
  
          foreach($params as $key =&gt; $value) {
              $queryParams[':' . $key] = $value;
          }
  
          //Execute Query
          if($preparedQuery-&gt;execute($queryParams)) {
  
          }
     }
  }
  </code></pre>
  
  <p>Calling <code>Database::Insert</code></p>
  
  <pre><code>Database::insert("test_table", [
      'username' =&gt; "Joe_Scotto",
      'name' =&gt; "Joe"
  ]);
  </code></pre>
  
  <p>This code does work, the only issue is that it runs twice. I think this has something to do with the class getting instantiated twice but am not sure. </p>