如何在PHP/MYSQL中将两个mysql查询作为一个查询执行? [英] How to execute two mysql queries as one in PHP/MYSQL?
问题描述
我有两个查询,如下所示:
I have two queries, as following:
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屋!