Skip

Does php fork standalone mysql queries or wait for them?
October 16, 2009 7:21 AM   Subscribe

How does PHP handle mysql requests (in regard to time and 'forking')

A piece of PHP code does a mysql INSERT. Assuming that there are no further mysql commands (ie, another SQL query or a request for the insert id), does PHP wait for the mysql to be completed before it continues with the rest of the PHP? Or does it wander on ahead?

The reason I ask, is that I have a server that is (occasionally) stacking a load of slow queries.

I've also got a system that does inserts a record, then redirects the browser to a different page which looks for a modified records.

I'm getting reports of a bug that could be a race condition, but I'd assumed that PHP was waiting for a response from mysql...
posted by twine42 to Computers & Internet (4 answers total)
 
The results of an INSERT should be available immediately. You can confirm this by doing an INSERT and then checking the return value of the mysql_insert_id() function. It will return the value from the AUTO_INCREMENT column of the new record.

So yes, PHP does wait for completion of the INSERT. I've never known it not to.
posted by le morte de bea arthur at 7:29 AM on October 16, 2009 [1 favorite]


Actually, let me qualify that a little. If you're using the MySQL Native Driver, as opposed to the usual MySQL Client Library, and if you use the MYSQLI_ASYNC resultmode in a call to the mysqli_query function (available in the MySQL Improved Extension), then yes, it is technically possible to run a MySQL query asynchronously.

But it's rather unlikely.
posted by le morte de bea arthur at 8:40 AM on October 16, 2009 [1 favorite]


Is the suspected race condition in a single request or multiple requests?

PHP won't continue to the next line of code until the INSERT is complete, but if two users are calling the same PHP script, there's no guarantee that the requests won't interleave.

If you insert a record then redirect a user, the user should not be redirected until that record is in the database. PHP does not "wander ahead".

But if you have two users accessing the same database via the same (or different) PHP scripts, there's no guarantee that their requests will be processed in a particular order relative to each other. It might go

INSERT USER1's record
INSERT USER2's record
REDIRECT USER2
REDIRECT USER1
posted by justkevin at 9:37 AM on October 16, 2009 [1 favorite]


Cool, that's what I hoped. Right, the problem is in the code not the screwy mysql server.
posted by twine42 at 9:47 AM on October 16, 2009


« Older What shouldn't we miss in Tene...   |  I'm looking for examples of fa... Newer »
This thread is closed to new comments.


Post