title: Why I am getting the Error "Commands out of sync; you can't run this command now"
body: <p>The Documentation of the Error Mentioned in the Title Says</p>
  
  <blockquote>
    <p>If you get <strong>Commands out of sync; you can't run this command now</strong> in
    your client code, you are calling client functions in the wrong order.</p>
    
    <p>This can happen, for example, if you are using mysql_use_result() and
    try to execute a new query before you have called mysql_free_result().
    It can also happen if you try to execute two queries that return data
    without calling mysql_use_result() or mysql_store_result() in between.</p>
  </blockquote>
  
  <p>From here: <a href="http://dev.mysql.com/doc/refman/5.0/en/commands-out-of-sync.html" rel="noreferrer">http://dev.mysql.com/doc/refman/5.0/en/commands-out-of-sync.html</a></p>
  
  <p>But In First Query I am not fetching any data from mysql database, I am just inserting. And In second Query I am getting the data from database.</p>
  
  <p>Here is My code</p>
  
  <pre><code>$connection = mysqli_connect("localhost","username","password","tbl_msgs");
  if(mysqli_connect_errno($connection))
  {
      die("Failed to connect to MySQL: " . mysqli_connect_error());
  }
  $query = "INSERT INTO users (total_comments, total_views) 
            VALUES ({$total_comments}, {$total_views});";
  
  $query .= "INSERT INTO msgs (notifications) VALUES ({$notifications})";
  
  mysqli_multi_query($connection,$query);
  </code></pre>
  
  <p>Upto this Step every thing is fine. But When I execute the following query It gives the Error</p>
  
  <pre><code>$select_query = "SELECT * FROM msgs WHERE msg_id = {$msg_id}";
  
  $result_set = mysqli_query($connection,$select_query);
  
  if(!$result_set) {
      die(mysqli_error($connection)); 
  }
  </code></pre>
  
  <p>Here it gives the Error <code>Commands out of sync; you can't run this command now</code>. I can't understand this situation</p>
  
  <p>Note: There is any Problem in the Query, I have executed the same query directly to PHPMyAdmin and it works fine.</p>