views:

673

answers:

4

In asp.net, you can retrieve MULTIPLE datatables from a single call to the database. Can you do the same thing in php?

Example:

$sql ="select * from t1; select * from t2;";

$result = SomeQueryFunc($sql);

print_r($result[0]); // dump results for t1

print_r($result[1]); // dump results for t2

Can you do something like this?

A: 

As far as I know, this is not possible. At least PDO (which I would recommend for accessing databases) doesn't allow this.

Simon Lehmann
A: 

This should be possible with newer MySQL and the mysqli (improved) php extension. I'm not sure if any DB abstraction layers support this.

See relevant MySQL docs and PHP docs.

gnud
A: 

PDOStatement::nextRowset() seems to be what you're after.

porneL
+2  A: 

This is called "multi-query." The mysql extension in PHP does not have any means to enable multi-query. The mysqli extension does allow you to use multi-query, but only through the multi_query() method. See http://php.net/manual/en/mysqli.multi-query.php

Using multi-query is not recommended, because it can increase the potential damage caused by SQL injection attacks. If you use multi-query, you should use rigorous code inspection habits to avoid SQL injection vulnerability.

Bill Karwin