title: Is it possible to run a mysqli query directly via the link while a statement is opened?
body: <p>I'm trying to do a simple operation on a MySQL database: my contacts have their complete names on a column called <code>first_name</code> while the column <code>last_name</code> is empty.</p>
  
  <p>So I want to take what's on the <code>first_name</code> column and split it on the first occurrence of a white space and put the first part on the <code>first_name</code> column and the second part on the <code>last_name</code> column.</p>
  
  <p>I use the following code but it's not working:</p>
  
  <pre><code>$connection = new mysqli(DATABASE_SERVER, DATABASE_USERNAME, DATABASE_PASSWORD, DATABASE_NAME, DATABASE_PORT);
  
  $statement = $connection-&gt;prepare("SELECT id, first_name FROM contacts");
  
  $statement-&gt;execute();
  
  $statement-&gt;bind_result($row-&gt;id, $row-&gt;firstName);
  
  while ($statement-&gt;fetch()) {
    $names = separateNames($row-&gt;firstName);
  
    $connection-&gt;query('UPDATE contacts SET first_name="'.$names[0].'", last_name="'.$names[1].'" WHERE id='.$row-&gt;id);
  }
  
  $statement-&gt;free_result();
  $statement-&gt;close();
  
  $connection-&gt;close();
  </code></pre>
  
  <p>Can I use the <code>$connection-&gt;query</code> while having the statement open?</p>
  
  <p>Best regards.</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>The <code>$connection-&gt;query(...)</code> returns <code>FALSE</code> and I get the following error:</p>
  
  <pre><code>PHP Fatal error:  Uncaught exception 'Exception' with message 'MySQL Error - 2014 : Commands out of sync; you can't run this command now'
  </code></pre>
  
  <p>I changed the code to the following and worked:</p>
  
  <pre><code>$connection = new mysqli(DATABASE_SERVER, DATABASE_USERNAME, DATABASE_PASSWORD, DATABASE_NAME, DATABASE_PORT);
  
  $result = $connection-&gt;query("SELECT id, first_name FROM contacts");
  
  while ($row = $result-&gt;fetch_row()) {
    $names = separateNames($row[1]);
  
    $connection-&gt;query('UPDATE contacts SET first_name="'.$names[0].'", last_name="'.$names[1].'" WHERE id='.$row[0]);
  }
  
  $connection-&gt;close();
  </code></pre>