如何在 PHP/MYSQL 中同时执行两个 mysql 查询? [英] How to execute two mysql queries as one in PHP/MYSQL?

查看:50
本文介绍了如何在 PHP/MYSQL 中同时执行两个 mysql 查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个查询,如下:

SELECT SQL_CALC_FOUND_ROWS Id, Name FROM my_table WHERE Name LIKE '%prashant%' LIMIT 0, 10;
SELECT FOUND_ROWS();

我想一次尝试执行这两个查询.

I want to execute both these queries in a single attempt.

$result = mysql_query($query);

然后告诉我我将如何分别处理每张桌子.实际上在 ASP.NET 中,我们使用数据集来处理两个查询

But then tell me how I will handle each tables set separately. Actually in ASP.NET we uses dataset which handles two queries as

ds.Tables[0];
ds.Tables[1]; .. etc

如何使用 PHP/MYSQL 做同样的事情?

How can I do the same using PHP/MYSQL?

推荐答案

更新: 显然可以通过将标志传递给 mysql_connect().请参阅使用 PHP 在一个语句中执行多个 SQL 查询 尽管如此,任何当前的读者都应该避免使用 mysql_ 类的函数而更喜欢 PDO.

Update: Apparently possible by passing a flag to mysql_connect(). See Executing multiple SQL queries in one statement with PHP Nevertheless, any current reader should avoid using the mysql_-class of functions and prefer PDO.

您不能使用 PHP 中的常规 mysql-api 来做到这一点.只需执行两个查询.第二个会如此之快,以至于无关紧要.这是微优化的典型例子.别担心.

You can't do that using the regular mysql-api in PHP. Just execute two queries. The second one will be so fast that it won't matter. This is a typical example of micro optimization. Don't worry about it.

为了记录,它可以使用 mysqli 和 mysqli_multi_query 来完成-函数.

For the record, it can be done using mysqli and the mysqli_multi_query-function.

这篇关于如何在 PHP/MYSQL 中同时执行两个 mysql 查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆