PHP to deal with concurrency, MySQL database to prevent data inconsistency

analysis:

PHP language itself is single-threaded, so in response to concurrent programming language level is no better solution for the above, but can be achieved by means of other designs, such as the database itself row lock to achieve MySQL-based Innodb the engine, such as a queue or rely on Redis storage to achieve. Here to explain implementation based MySQL row lock.

 

note:

Only Innodb based engine is supported database transaction row lock, if in actual operation, the execution is not successful, check the list engine is correct. In addition row locks rely on data submitted in the transaction commits.

 

code:

conn $ = mysql_connect ( '127.0.0.1', 'root', 'root') or Die ( mysql_error ()); // links MySQL 

mysql_select_db ( $ conn , 'Student'); // Select the database 

mysql_query ( $ conn , 'BEGIN'); // open the transaction 

can be accessed by another process after the // execute this statement, username = "John Doe" line in the transaction will be submitted to the 
$ RES = mysql_query ( $ conn , 'the FROM the SELECT * user WHERE username = "John Doe" the UPDATE the FOR ' ); 

$ Row = the mysql_fetch_array ( $ RES );// get the result set 

mysql_query (conn $ , "the SET Update the User username = 'John Doe' where username = 'Joe Smith' '); // re-update data 

mysql_query ( $ conn ,' COMMIT '); // commit the transaction 

mysql_free_result ( $ RES ); 
 mysql_close ( conn $ );

From the micro-channel public number: Programming Society

Advanced programmers daily book, please pay attention!

Guess you like

Origin www.cnblogs.com/ai10999/p/11449479.html