How can the LOCK TABLE command be used to control access to a table in PHP?

The LOCK TABLE command in PHP can be used to control access to a table by preventing other sessions from accessing the table for reading or writing while the lock is in place. This can be useful in situations where you want to ensure exclusive access to a table for a specific operation.

// Establish a connection to the database
$connection = new mysqli("localhost", "username", "password", "database");

// Lock the table for writing
$connection->query("LOCK TABLES table_name WRITE");

// Perform your operations on the table

// Unlock the table
$connection->query("UNLOCK TABLES");

// Close the database connection
$connection->close();