title: Commands out of sync; you can't run this command now
body: <p>I am trying to execute my PHP code, which calls two MySQL queries via mysqli, and get the error "Commands out of sync; you can't run this command now".</p>
  
  <p>Here is the code I am using</p>
  
  <pre><code>&lt;?php
  $con = mysqli_connect("localhost", "user", "password", "db");
  if (!$con) {
      echo "Can't connect to MySQL Server. Errorcode: %s\n". Mysqli_connect_error();
      exit;
  }
  $con-&gt;query("SET NAMES 'utf8'");
  $brand ="o";
  $countQuery = "SELECT ARTICLE_NO FROM AUCTIONS WHERE upper(ARTICLE_NAME) LIKE % ? %";
  if ($numRecords = $con-&gt;prepare($countQuery)) {
      $numRecords-&gt;bind_param("s", $brand);
      $numRecords-&gt;execute();
      $data = $con-&gt;query($countQuery) or die(print_r($con-&gt;error));
      $rowcount = $data-&gt;num_rows;
      $rows = getRowsByArticleSearch("test", "Auctions", " ");
      $last = ceil($rowcount/$page_rows);
  }  else {
  
  print_r($con-&gt;error);
  }
  foreach ($rows as $row) {
      $pk = $row['ARTICLE_NO'];
      echo '&lt;tr&gt;' . "\n";
      echo '&lt;td&gt;&lt;a href="#" onclick="updateByPk(\'Layer2\', \'' . $pk . '\')"&gt;'.$row['USERNAME'].'&lt;/a&gt;&lt;/td&gt;' . "\n";
      echo '&lt;td&gt;&lt;a href="#" onclick="updateByPk(\'Layer2\', \'' . $pk . '\')"&gt;'.$row['shortDate'].'&lt;/a&gt;&lt;/td&gt;' . "\n";
      echo '&lt;td&gt;&lt;a href="#" onclick="deleterec(\'Layer2\', \'' . $pk . '\')"&gt;DELETE RECORD&lt;/a&gt;&lt;/td&gt;' . "\n";
      echo '&lt;/tr&gt;' . "\n";
  }
  function getRowsByArticleSearch($searchString, $table, $max) {
      $con = mysqli_connect("localhost", "user", "password", "db");
      $recordsQuery = "SELECT ARTICLE_NO, USERNAME, ACCESSSTARTS, ARTICLE_NAME, date_format(str_to_date(ACCESSSTARTS, '%d/%m/%Y %k:%i:%s'), '%d %m %Y' ) AS shortDate FROM AUCTIONS WHERE upper(ARTICLE_NAME) LIKE '%?%' ORDER BY str_to_date(ACCESSSTARTS, '%d/%m/%Y %k:%i:%s')" . $max;
      if ($getRecords = $con-&gt;prepare($recordsQuery)) {
          $getRecords-&gt;bind_param("s", $searchString);
          $getRecords-&gt;execute();
          $getRecords-&gt;bind_result($ARTICLE_NO, $USERNAME, $ACCESSSTARTS, $ARTICLE_NAME, $shortDate);
          while ($getRecords-&gt;fetch()) {
              $result = $con-&gt;query($recordsQuery);
              $rows = array();
              while($row = $result-&gt;fetch_assoc()) {
                  $rows[] = $row;
              }
              return $rows;
          }
      }
  }
  </code></pre>
  
  <p>I have tried reading up on this, but I am unsure of what to do. I have read about store result and free result, however these have made no difference when using them. I am unsure at exactly which point this error is being caused, and would like to know why it is being caused, and how to fix it.</p>
  
  <p>Going by my debug statements, the first if loop for countQuery is not even being entered, because of an error in my sql syntax near near <code>'% ? %'</code>. However if I just select <code>*</code> instead of trying to limit based on a LIKE clause, I still get the command out of sync error.</p>