0

If I use a large function that will update data on MySQL and then execute header("Location: somepage"), will PHP wait before all those update queries are done and then redirect or do I have to account for that?

0

3 Answers 3

4

Yes, PHP will sequentially execute each command and will only reach the redirect statement once the update queries have reported success.

Sign up to request clarification or add additional context in comments.

Comments

4

PHP code is executed in a predicable sequential pattern, top down.

If you run some database queries the database server will handle them, PHP's thread doesn't handle the writing itself (just passes the query over).

MyISAM tables are locked on write and and I'm pretty sure the UPDATE queries are atomic (meaning they either all do or don't happen - they don't leave things half finished).

Also, slightly tangential, don't forget to exit after sending the Location header. User agents are free to ignore the Location header.

4 Comments

What if it's a move_uploaded_file() before a header()? Will php wait for the upload before redirecting?
@user1091856: That should translate to some system calls which will be called before the header is sent back to the browser (which is irrelevant to moving a file on the server).
Yes. More accurately, the upload will have finished before the PHP code even executes.
move_uploaded_file() and mysql_query() are both "blocking" methods -- as are most PHP methods. This means the method "blocks" execution until the method is complete, so no other code will run. PHP's documentation will clearly mark "non-blocking" methods which return control before they've completed their work -- these are the exception, not the rule.
1

From the php documentation

For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.

Then... if it return the success or failure, it must wait until the update has finished.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.