何时以及如何通过 PHP 使用多个 MySQL 查询 (PDO)

发布于 2024-07-15 08:33:39 字数 107 浏览 8 评论 0原文

除了最大限度地减少代码之外,在一个语句中使用多个 MySQL 查询还有什么好处。

如何使用 PHP(最好是 PDO)执行、检索和显示在一条语句中发送的多个 MySQL 查询的结果。

What are the benefits of using multiple MySQL queries in a statement, other than to minimize code.

How do you execute, retrieve, and display the results of multiple MySQL queries sent in one statement using PHP (and preferably PDO).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

mysql_query() 不支持多个查询。 但是,有一些解决方法:

http://www.dev-explorer.com/articles/multiple-mysql-queries
http://php.net/function.mysql-query

mysql_query() doesn't support multiple queries. However, there are some workarounds:

http://www.dev-explorer.com/articles/multiple-mysql-queries
http://php.net/function.mysql-query

分分钟 2024-07-22 08:33:39

无论您使用哪个数据库,将多个查询放入一个语句中都会更有效。 如果单独执行查询,则必须通过网络(或者至少在同一台计算机上的进程之间)调用数据库,获取到数据库的连接(包括身份验证),传入查询,返回结果集,并释放每个查询的连接。

即使您使用连接池,您仍然会来回传递比需要的更多的请求。

因此,例如,如果您将两个查询合并为一个,则您就可以节省第二个查询的所有额外来回调用。 组合的查询越多,您的应用程序就越高效。

再例如,许多应用程序在启动时都会填充列表(例如下拉列表)。 这可能是许多查询。 在一次调用中执行所有这些操作可以加快启动时间。

Regardless of which database you are using, it can be more efficient to put multiple queries into one statement. If you perform the queries separately, you have to make a call to the database across the network (or at least, between processes if on the same machine), get a connection to it (including autheticating), pass in the query, return the resultset, and release the connection for each query.

Even if you use connection pooling, you are still passing more requests back and forth than is necessary.

So, for example, if you combine two queries into one, you have saved all of that extra calling back and forth for the second query. The more queries you combine, then, the more efficient your app can become.

For another example, many apps populate lists (such as for dropdownlists) when they start up. That can be a number of queries. Performing them all in one call can accelerate startup time.

你另情深 2024-07-22 08:33:39

我今晚做了一些研究,偶然发现了这个线程。 上述答案都没有为这个简单的问题提供可行的解决方案。

使用面向对象方法,您可以使用 multi_query() 方法在单个连接中执行多个查询语句。

$db_connection->multi_query($query);

查询应该格式正确并用分号分隔 -> ;

有关如何处理 SELECT 数据的更多详细信息,请参阅 PHP 手册

I was doing some research tonight and stumbled across this thread. None of the answers above provide a viable solution to this simple problem.

Using the OO Approach, you can perform multiple query statements in a single connection by using the multi_query() method.

$db_connection->multi_query($query);

Queries should be well-formed and separated by semicolons -> ;

see The PHP manual for more details on how to handle SELECT data

尛丟丟 2024-07-22 08:33:39

当您需要使用会话变量和临时表时,同一调用上的多个查询特别有用:

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;

Multiple queries on the same call are especially beneficial 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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文