When and how to use multiple MySQL queries with PHP (PDO)

What are the benefits of using multiple MySQL queries in an instruction other than code minimization.

How to execute, retrieve and display the results of multiple MySQL queries sent in a single expression using PHP (and preferably PDO).

+5
source share
4 answers

No matter which database you use, it may be more efficient to place multiple queries in a single expression. If you execute queries separately, you must make a call to the database over the network (or at least between processes, if on the same computer), get a connection to it (including auto-rating), send the request, return the resultset and release connection for each request.

Even if you use the connection pool, you still send more requests back and forth than necessary.

So, for example, if you combine the two queries into one, you saved all these additional calls back and forth for the second query. The more requests you combine, the more efficient your application can be.

(, dropdownlists), . . .

+4

. .

OO, multi_query().

$db_connection->multi_query($query);

โ†’ ;

. PHP , SELECT

+1
source

Several queries for the same call are especially useful when you need to work with session variables and temporary tables:

select somecol, @somevar := group_concat( distinct somecol2 separator '|')
from sometbl
where
    somecol LIKE "fueh"
group by somecol;

select somecol2
from  sometbl
where
    somecol LIKE "nyan"
and
    vc_wp regexp concat( '(', @somevar, ')' )
order by somecol;
0
source

All Articles