title: PHP mysqli Commands out of sync; you can't run this command now
body: <p>In my db I have some stored procedures.</p>
  
  <p>I want to call the one of them, and according to the results I get from this procedure, i will determine weather to call the second one or not.</p>
  
  <p>As you can see in the <code>isUserValid()</code> method I've used <code>$result-&gt;free_results()</code></p>
  
  <p>but still when calling the other procedure I get: <code>Commands out of sync; you can't run this command now</code></p>
  
  <p>This is my calling to the db functions:</p>
  
  <pre><code>    /**
       * Run a new query on the db.
       * this will open an close the connection to the db
       * @param $query string the query to run
       * @param $verifyAccessToken boolean true if user validation is needed before query is executed, false otherwise
       * @return bool|mysqli_result the result of the query if any or true or false in a non query
       * @throws Exception on connection and query errors
       */
      private function queryDb($query,$verifyAccessToken)
      {
         // if ($this-&gt;test)
  //            echo $query . "&lt;br&gt;";
          //create a new mysqli connection
          $mysqli = new mysqli($this-&gt;DB_HOST, $this-&gt;DB_USER, $this-&gt;DB_PASS, $this-&gt;DB_NAME);
          //set the charset for the connection
          if (!$mysqli-&gt;set_charset("utf8"))
          {
              $mysqli-&gt;close();
              throw new Exception ("Error setting UTF 8 DB connection");
          }
          //check if the connection was ok
          if (mysqli_connect_errno())
          {
              $mysqli-&gt;close();
              throw new Exception("Error connecting DB");
          }
  
          //if verification is needed - verify access token
         if ($verifyAccessToken &amp;&amp; !$this-&gt;isUserValid($mysqli))
         {
            $mysqli-&gt;close();
            throw new Exception(ACCESS_TOKEN_INCORRECT);
         }
  
          //get results
          $result = $mysqli-&gt;query($query);
          //check if there were now error in query. if so - throw an exception
          if (!$result)
          {
              $mysqlError = $mysqli-&gt;error . __LINE__;
              //close the connection
              $mysqli-&gt;close();
              throw new Exception ("mySQL Error: " . $mysqlError);
          }
          //fetch the results into an array
          $rows = $result-&gt;fetch_all(MYSQLI_BOTH);
          //close the connection
          $result-&gt;close();
          $result-&gt;free();
          $mysqli-&gt;close();
          //return the results if any
          return $rows;
      }
  
      /**
       * Check if user is valid
       * @param $mysqli mysqli connection to db
       * @return true if user is valid, false otherwise
       */
      function isUserValid($mysqli)
      {
          //Check if there is a saved access token. if not - throw an exception
          if (is_null($this-&gt;user) || $this-&gt;user-&gt;getPicoAccessToken() == null)
            throw new Exception(ACCESS_TOKEN_INCORRECT);
          //get user details
          $facebookId = $this-&gt;user-&gt;getFacebookId();
          $picoAccessToken = $this-&gt;user-&gt;getPicoAccessToken();
          //create verification query
          $verificationQuery = "Call isUserValid('$facebookId','$picoAccessToken')";
          //execute query
          $result = $mysqli-&gt;query($verificationQuery);
  
          //if query had error - throw exception
          if (!$result)
          {
           $mysqlError = $mysqli-&gt;error . __LINE__;
           //close the connection
           $mysqli-&gt;close();
           throw new Exception ("mySQL Error: " . $mysqlError);
          }
          $rows = $result-&gt;fetch_all(MYSQLI_ASSOC);
          $rows = $rows[0];
          $result-&gt;free_result();
          //return user valid status
          return isset($rows["isUserValid"]) &amp;&amp; $rows["isUserValid"] == 1;
      }
  </code></pre>