I am having a few issues when people are trying to access a MySQL database and they are trying to update tables with the same information.
I have a webpage written using PHP. In this webpage is a query to check if certain data has been entered into the database. If the data hasn’t, then i proceed to insert it. The trouble is that if two people try at the same time, the check might say the data has not been entered yet but when the insert takes place it has been by the other person.
What is the best way to handle this scenario? Can i lock the database to only process my queries first then anothers?
You’re looking for
LOCK.http://dev.mysql.com/doc/refman/5.0/en/lock-tables.html
This can be run as a simple
mysql_query(orMySQLi::query/prepare).I’d say it’s better to lock specific tables (although you can probably try
LOCK TABLES *) that need to be locked rather than the whole database – as nothing will be able to be read. I think you’re looking for something like:Or in PHP: