title: PHP Commands out of sync; you can't run this command now
body: <p>I've seen this question before but all the solutions do not work for me. The main solution is to store the result which I already do. Most people say I am running 2 simultaneous queries which I don't understand. I may have used this function below more than once but I always free and close the statement so not sure why this is happening.</p>
  
  <p>You can most likely ignore the top half of the function which just generates a string to represent the types. This is the full DB class I made:</p>
  
  <p>(I edited it since I originally posted it and replaced self::$connection with self::$db)</p>
  
  <pre><code>class DB {
      private static $dbhost = "localhost";
      private static $dbuser = "some_user"; // placeholder
      private static $dbpass = "some_assword"; // placeholder
      private static $dbname = "database_name"; // placeholder
      private static $db;
  
      public static function connect() {          
          self::$db = new mysqli(self::$dbhost,  self::$dbuser, self::$dbpass, self::$dbname);        
          if (self::$db-&gt;connect_errno) {
              die("Database mysqli failed: " .
                  self::$db-&gt;connect_error . " (" . 
                  self::$db-&gt;connect_errno . ")"      
              );
          }
      }
  
      // IGNORE THIS! It just formats the parameters for the 
      // call_user_func_array function to work correctly.
      private static function getBindings(&amp;$params) {
          $types = "";        
          $bindings = array();
          foreach ($params as $value) {
              switch (gettype($value)) {
                  case "integer":
                      $types .= "i";
                      break;
                  case "double":
                      $types .= "d";
                      break;
                  case "string":
                      $types .= "s";
                      break;
                  default: 
                      $types .= "s";
                      break;
              }
          }       
          foreach($params as $key =&gt; $value)
              $bindings[$key] = &amp;$params[$key]; // assign to references (because bind_param requires references)      
          // to add a string of variable types to the start of the $bindings array (such as 'sss')
          array_unshift($bindings, $types);
          return $bindings;
      }
  
      public static function query($query, $params) {
          if (!isset(self::$db)) { self::connect(); }
  
          if (empty($params)) {
              // prepared statement not needed
              return self::$db-&gt;query($query);
          }
  
          $successful = false;
          $bindings = self::getBindings($params);
  
          // MySQL prepared statement execution:
          $statement = self::$db-&gt;prepare($query);
          call_user_func_array(array($statement, 'bind_param'), $bindings);       
          $statement-&gt;execute();
          $statement-&gt;store_result(); 
  
          if ($statement-&gt;num_rows &gt; 0) {
              // for select queries
              $successful = $statement-&gt;get_result(); // does not work! (returns boolean)
  
              echo self::$db-&gt;errno; // 2014          
              echo "&lt;br /&gt;";
              echo self::$db-&gt;error; // Commands out of sync; you can't run this command now
              echo "&lt;br /&gt;";      
  
              // this method works fine (but I need to return the result set!):
              /*$name = false; $link = false;
              $statement-&gt;bind_result($name, $link);
  
             while ($statement-&gt;fetch()) {
                  echo 'name: '.$name.'&lt;br&gt;';
                  echo 'link: '.$link.'&lt;br&gt;';
             }*/
          } else if ($statement-&gt;affected_rows &gt; 0) {
              // for insert queries
              $successful = true;
          }       
  
          $statement-&gt;free_result();
          $statement-&gt;close();
          return $successful;
      }
  
      public static function close() {
          if (isset(self::$db)) self::$db-&gt;close();
      }
  }
  </code></pre>
  
  <p>EDIT: This is what one of my requests looks like (I have queried 2 requests on this same page using my DB class and DB::query(...) function):</p>
  
  <pre><code>$result = DB::query("SELECT * FROM table_name;");
  if ($result) {
      while ($row = $result-&gt;fetch_assoc()) {
          // do stuff                             
      }
      $result-&gt;close();
  }
  </code></pre>