title: MySQL prepared statement vs normal query. Gains & Losses
body: <p>I'm in the middle of updating/reworking some database code and I was wondering, what I should <em>really</em> expect from using prepared statements.  </p>
  
  <p>Take this example code:  </p>
  
  <pre><code>$values = '';
  for ($i = 0; $i &lt; $count; $i++) {
      $name = mysql_real_escape_string ($list[$i][1]);
      $voc = mysql_real_escape_string ($list[$i][3]);
      $lev = $list[$it][2];
      $lev = is_numeric ($lev)? $lev : 0;
  
      $values .= ($values == '')? "('$name', '$voc', $lev)" : ", ('$name', '$voc', $lev)";
  }
  if ($values != '') {
      $core-&gt;query ("INSERT INTO onlineCList (name, voc, lev) VALUES $values;");
  }
  </code></pre>
  
  <p>Now, apart from the obvious gain in readability (, sanity) and the fact that <code>max_packet_size</code> stops being an issue, am I supposed to expect any changes in performance when I recode this to use prepared statements? I'm connecting remotely to the MySQL server, and I worry that sending multiple small packets would be significantly slower then sending one big packet. If this is the case, can MySQLi/mysqlnd cache these packets?</p>
  
  <p>Another example:</p>
  
  <pre><code>$names = '';
  while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {
      $name = mysql_real_escape_string($row['name']);
  
      $names .= ($names == '') ? "'$name'" : ", '$name'";
  }
  if ($names != '') {
      $core-&gt;query ("UPDATE onlineActivity SET online = NULL WHERE name IN ($names) AND online = 1;");
  }
  </code></pre>
  
  <p>As above, should I expect the unexpected, after recoding this to use prepared statements? Does it make any difference for the MySQL server, if it has to run one query with a big IN clause, or multiple prepared queries with equality checks (<code>.. WHERE name = $name AND ..</code>)?</p>
  
  <p>Assume that everything is properly indexed.</p>