title: How to write a PHP + MySQL web service to perform 1000 requests (INSERT and SELECT) per second
body: <p>I have a table with fields such as <code>id</code>, <code>type</code>, <code>data</code>, etc... I need to write a web service script in PHP which should insert data to table, then select last 10 record with same type and return in HTTP body.</p>
  
  <p>1000 instances need to access this script per second. </p>
  
  <p>My question is what is the best way to do it?</p>
  
  <p>Here is my code. I am not sure this is good practice and could do with an example of how to make it better... </p>
  
  <pre><code>// Connect to mysql server
  $link = mysql_connect(HOST, USER, PASSWORD);
  if(!$link) {
      die('Could not connect to server: ' . mysql_error());
  }
  
  // Select database
  $db = mysql_select_db(DATABASE);
  if(!$db) {
      die('Cannot use the database');
  }
  mysql_set_charset('charset=utf8', $link); 
  
  // Insert data
  $query = "INSERT INTO `mytable` (`id`, `type`, `data`) VALUES ('$id', '$type', '$data')";   
  mysql_query($query);
  
  // Select data
  $query = "SELECT * FROM `mytable` WHERE `type`='$type' ORDER BY DESC LIMIT 10";
  $result = mysql_query($query);
  
  while($row = mysql_fetch_assoc($result)) {
  
      // Create xml to return
  
  }
  </code></pre>