Multiple data tables in PHP / MySQL?

In asp.net you can get MULTIFUNCTIONAL data from one call to the database. Can you do the same 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?

+3
source share
4 answers

This is called multi-query. The mysql extension in PHP has no means to enable multitasking. The mysqli extension allows you to use multiple queries, but only through the multi_query () method. See http://php.net/manual/en/mysqli.multi-query.php

, , SQL-. , , SQL-.

+3

MySQL php mysqli (). , - DB.

. MySQL PHP docs.

0

PDOStatement::nextRowset() It seems that you are after.

0
source

If you are using classic MySQL, you cannot. You can create a function that looks 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

0
source

Source: https://habr.com/ru/post/1698223/


All Articles