3

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?

genesis
  • 50,477
  • 20
  • 96
  • 125
John
  • 32,403
  • 80
  • 251
  • 422

4 Answers4

3

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
  • 538,548
  • 86
  • 673
  • 828
0

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
  • 77,584
  • 5
  • 64
  • 78
0

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

Nightfirecat
  • 11,432
  • 6
  • 35
  • 51
Kornel
  • 97,764
  • 37
  • 219
  • 309
0

If you're using classic MySQL, you can't. You can create a function which will look like

function SomeQueryFunc($queries) {
    $queries = explode(';', $queries);
    $return = array();
    foreach($queries as $index => $query) {
        $result = mysql_query($query);
        $return[$index] = array();
        while($row = mysql_fetch_assoc($result)) {
           foreach($row as $column => $value) {
              $return[$index][$column] = $value;
           }
        }
    }
    return $return;
}

which will work as expected

genesis
  • 50,477
  • 20
  • 96
  • 125