简体   繁体   中英

How frequent can I update a row of MySQL database through PHP?

I have created a page which allows clients to change the background color. For example, 5 background colors are offered to be chosen and some buttons are made for changing the background color. The server will save what background color the clients chose in the database so that they will see the background color they chose without any setting when they visit the page again. When the client clicks the button, a AJAX request will send to the server and the PHP script will query and update a row in the database to record the choice of the client.

Actually, I have done the page and nothing goes wrong if the client does not change the background color so frequently. For example, if I click to change from red to blue to green to white in 4 seconds evenly, the 3 changes are recognized and finally the "white" will be saved in the database. However, if I do that again in a very period, say, less than 1 second, I find that some updates failed. In details, I check it by if $stmt->affected_rows returns 0 and, for instance, it may show the updates corresponding to "green" and "white" has affected rows 0.

If some updates failed, the last background color the client chose may not be saved. Also, it is strange to limit how fast the client can change the background. Hence, I wonder the reasons cause the problem.

Does it reach fastest update frequency? Or, is it because the last update is still writing the data and locking the row? Can MySQL receive the update queries and make them into a queue, and process them sequentially?

I have a feeling that your where clause is including the old color value. Because of network race conditions, you can't guarantee the order that your AJAX requests will arrive on the server. There is nothing inherent in PHP or MySQL that would limit the time between UPDATE statements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM