views:

118

answers:

4

Suppose I am executing several queries on the server using mysql_query. The results of every query affect the subsequent query. Will every call of mysql_query be completely executed before control moves on to the next one?

Edit: I forgot to mention, I am not using a transactional storage engine.

+4  A: 

Yes, the MySQL server must return data and complete the query before PHP will progress to the next action, be it assigning the return value or progressing to the next line of code.

mysql_query( "INSERT INTO y VALUES (x,1)" );
mysql_query( "SELECT x FROM y WHERE z=1" );
mysql_query( "UPDATE y SET x=x+1 WHERE z=1" );
mysql_query( "SELECT x FROM y WHERE z=1" );

x will be 1, then 2, and by the time of the fourth statement 2 will be returned to the script. It is not possible to run the queries in parallel in a single PHP script - this would require parallel execution or threading in another language (i.e. Java).

Andy
Are there multi-threading libraries for PHP?
Marcus Adams
Nope There is no multi-Threading Concept in PHP ALas!! :(
OM The Eternity
http://php.net/manual/en/function.pcntl-fork.php Multi-threading Example: http://www.php.net/manual/en/ref.pcntl.php#77603 N.B: pcntl is not enabled by default and won't work on Windows platforms.
Phill Pafford
+1  A: 

In the same script and thread, yes. Other queries from other PHP scripts may be running at the same time, but all queries from your php script will block.

You will always be able to evaluate the return value before moving on.

Marcus Adams
A: 

When you are not using transactions, MySQL will commit every query instantly, regardless of the programming language you used. So yes, you can rely on the sequentiality of your queries.

chiborg
+1  A: 

As others have already mentioned, PHP is a sequential language (ie one statement will be executed after the next) with no explicit support for threading. What you can do however is to use mysql_unbuffered_query, which will block only until the first row of the result is returned (as opposed to waiting for all the rows as mysql_query does).

Cd-MaN