susmits
susmits

Reputation: 2238

Is mysql_query under PHP a blocking function?

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.

Upvotes: 5

Views: 2195

Answers (5)

user1278519
user1278519

Reputation: 891

This doesn't answer the original question, but is related to the question headline:

If you want non-blocking, there's a quasi-option that works:

  1. for INSERTs only
  2. only for MyISAM, MEMORY, and ARCHIVE tables
  3. it has some overhead / possible performance loss on the insert

That's to use the INSERT DELAYED syntax in your query. Then, PHP will only wait for mysql to respond "OK" and will not wait for the actual insert to happen.

http://dev.mysql.com/doc/refman/5.0/en/insert-delayed.html

Upvotes: 2

Grey Panther
Grey Panther

Reputation: 13118

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).

Upvotes: 1

chiborg
chiborg

Reputation: 28084

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.

Upvotes: 0

Marcus Adams
Marcus Adams

Reputation: 53830

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.

Upvotes: 1

Andy
Andy

Reputation: 17771

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).

Upvotes: 6

Related Questions