PHP/MySQL中的多个数据表?

时间:2022-06-25 16:58:33

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

在asp.net中,您可以从对数据库的单个调用中检索多个数据。你能用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?

你能做这样的事吗?

4 个解决方案

#1


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

这就是所谓的“多查询”。PHP中的mysql扩展没有启用多查询的任何方法。mysqli扩展允许您使用多查询,但只能通过multi_query()方法。参见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.

不建议使用多查询,因为它会增加SQL注入攻击造成的潜在损害。如果您使用多查询,您应该使用严格的代码检查习惯来避免SQL注入漏洞。

#2


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.

这应该可以通过更新的MySQL和mysqli(改进的)php扩展实现。我不确定是否有任何DB抽象层支持这一点。

See relevant MySQL docs and PHP docs.

请参阅相关的MySQL文档和PHP文档。

#3


0  

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

PDOStatement::nextRowset()似乎是您所追求的。

#4


0  

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

如果你使用的是经典的MySQL,那就不行。您可以创建一个看起来像这样的函数

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

哪一个会像预期的那样工作

#1


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

这就是所谓的“多查询”。PHP中的mysql扩展没有启用多查询的任何方法。mysqli扩展允许您使用多查询,但只能通过multi_query()方法。参见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.

不建议使用多查询,因为它会增加SQL注入攻击造成的潜在损害。如果您使用多查询,您应该使用严格的代码检查习惯来避免SQL注入漏洞。

#2


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.

这应该可以通过更新的MySQL和mysqli(改进的)php扩展实现。我不确定是否有任何DB抽象层支持这一点。

See relevant MySQL docs and PHP docs.

请参阅相关的MySQL文档和PHP文档。

#3


0  

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

PDOStatement::nextRowset()似乎是您所追求的。

#4


0  

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

如果你使用的是经典的MySQL,那就不行。您可以创建一个看起来像这样的函数

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

哪一个会像预期的那样工作