title: Php concurrency mysqli
body: <p>I have a database on XAMPP and I use php in order to manipulate the database. I wish to read and write the database in concurrency way and each table are connected with the others. I don't understand if my approach is right.</p>
  
  <p>When I wish to read I use this mode:</p>
  
  <pre><code>        $connection = new mysqli($host, $user, $password, $dbName);
          if (connection-&gt;connect_errno)
            return false;
          $connection-&gt;autocommit(true);
          $result = mysqli_query($connection, $query);
          if (!$result)
              return false;
          else
              return true;
         $connection-&gt;close();
  </code></pre>
  
  <p>And when I wish to write I use <code>$connection-&gt;autocommit(false);</code> with commit or rollback at the end.</p>
  
  <p>But I wish to know if some users is reading and in this moment one or more people write on the database, does the database remain in right? And there is a way to put in an queue the write/read in order don't stop the user but putting in an spin wait?</p>
  
  <p>I have also read that mysqli has <code>mysqli_begin_transaction</code> so I have thought that it is possibile to use <code>mysqli_begin_transaction</code>when I have to write, but I'm not sure that it is right.</p>
  
  <p>Have you some ideas in order to read and write on the database in concurrency way without to stop any transaction but put in an spin wait queue?</p>
  
  <p>I have thought a way like mutex like the c with some lock (I wish to implement at the end this):</p>
  
  <ul>
  <li><p>I wish to write:</p>
  
  <ol>
  <li>I wait the lock in order to write and to block the other user;</li>
  <li>I lock the database and the others remains on spin wait;</li>
  <li>Here to check using php if the new records are right doing new query into the database but the database is locked only for user not for checking.</li>
  <li>Write database;</li>
  <li>unlock database.  </li>
  </ol></li>
  <li><p>When I wish to read:</p>
  
  <ol>
  <li>I wait if somebody finish to write;</li>
  <li>I read;</li>
  </ol></li>
  </ul>