Is mysql_query under PHP a blocking function?

Suppose I execute several queries on the server using mysql_query . The results of each query affect the subsequent query. Will any mysql_query call be fully executed before the control moves to the next?

Edit: I forgot to mention, I do not use transactional storage mechanism.

+4
source share
5 answers

Yes, the MySQL server must return data and fill out the query before PHP proceeds to the next step, assigning a return value or moving 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 in the script. It is impossible to run queries in parallel in one PHP script - this will require parallel execution or streaming use in another language (for example, Java).

+5
source

This does not answer the original question, but refers to the title of the question:

If you want to non-block, there is a quasi-option that works:

  • for INSERT only
  • Only for MyISAM, MEMORY, and ARCHIVE tables.
  • it has some overhead / possible performance loss on insert

To use the INSERT DELAYED syntax in your query. Then, PHP will wait until mysql answers “OK” and will not wait until the actual insert occurs.

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

+2
source

In the same script and stream, yes. Other requests from other PHP scripts may be executed at the same time, but all requests from your PHP script will be blocked.

You can always evaluate the return value before moving on.

+1
source

As already mentioned, PHP is a sequential language (i.e., one statement will be executed after the next) without explicit support for streaming. However, you can use mysql_unbuffered_query , which will only be blocked until the first row of the result is returned (as opposed to waiting for all rows as mysql_query).

+1
source

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

0
source

Source: https://habr.com/ru/post/1307140/


All Articles