title: Concurrent database access
body: <p>When the web server receives a request for my PHP script, I presume the server creates a dedicated process to run the script. <strong>If, before the script exits, another request to the same script comes, another process gets started -- am I correct, or the second request will be queued in the server, waiting for the first request to exit? (Question 1)</strong></p>
  
  <p>If the former is correct, i.e. the same script can run simultaneously in a different process, then they will try to access my database.</p>
  
  <p>When I connect to the database in the script:</p>
  
  <pre><code>$DB = mysqli_connect("localhost", ...);
  </code></pre>
  
  <p>query it, conduct more or less lengthy calculations and update it, I don't want the contents of the database to be modified by another instance of a running script.</p>
  
  <p><strong>Question 2: Does it mean that since connecting to the database until closing it:</strong></p>
  
  <pre><code>mysqli_close($DB);
  </code></pre>
  
  <p><strong>the database is blocked for any access from other software components?</strong> If so, it effectively prevents the script instances from running concurrently.</p>
  
  <p><strong>UPDATE:</strong> @OllieJones kindly explained that the database was not blocked.</p>
  
  <p>Let's consider the following scenario. The script in the first process discovers an eligible user in the Users table and starts preparing data to append for that user in the Counter table. At this moment the script in the other process preempts and deletes the user from the Users table and the associate data in the Counter table; it then gets preempted by the first script which writes the data for the user no more existing. These data become in the head-detached state, i.e. unaccessible.</p>
  
  <p>How to prevent such a contention? </p>