简体   繁体   English

PHP / MySQL中的多个数据表?

[英]Multiple Data Tables in PHP/MySQL?

In asp.net, you can retrieve MULTIPLE datatables from a single call to the database. 在asp.net中,您可以从一次调用数据库中检索MULTIPLE数据表。 Can you do the same thing in php? 你能在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? 你能做这样的事吗?

This is called "multi-query." 这称为“多查询”。 The mysql extension in PHP does not have any means to enable multi-query. PHP中的mysql扩展没有任何启用多查询的方法。 The mysqli extension does allow you to use multi-query, but only through the multi_query() method. mysqli扩展允许您使用多查询,但只能通过multi_query()方法。 See http://php.net/manual/en/mysqli.multi-query.php 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. 建议不要使用多查询,因为它可能会增加SQL注入攻击造成的潜在损害。 If you use multi-query, you should use rigorous code inspection habits to avoid SQL injection vulnerability. 如果使用多查询,则应该使用严格的代码检查习惯来避免SQL注入漏洞。

This should be possible with newer MySQL and the mysqli (improved) php extension. 这应该可以使用更新的MySQL和mysqli(改进的)php扩展。 I'm not sure if any DB abstraction layers support this. 我不确定是否有任何DB抽象层支持这一点。

See relevant MySQL docs and PHP docs . 查看相关的MySQL文档PHP文档

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

If you're using classic MySQL, you can't. 如果您使用的是经典MySQL,则不能。 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 这将按预期工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM