php - mysqli 链接正在掉线

标签 php mysql mysqli

我有一个函数接受 2 个变量,一个用于数据库连接,另一个用于查询。出于某种原因,mysqli 链接不满意,除非它在函数内部;仅传递链接是不够的。

有没有其他人发生过这种情况,我该怎么做才能避免在函数内部实例化 db 对象。

这不起作用,但如果我将对象放入函数中,它就起作用了。


function test($db, $query)
{
  if($db->multi_query($query)){
    do{
      if($result = $db->store_result()){
        while($row = $result->fetch_row()){
          print_r($row);
        }
        $result->free_result();
        $result->close();
      }
      if($db->more_results()){
        // print something here.
      }
    }while($db->next_result());
  }
  $db->close();
}

我是这样称呼他们的

$db = new mysqli('xxxx','xxxx','xxxx','xxxx');
$q1 = ("SELECT * FROM table1");
$q2 = ("SELECT * FROM table2");

test($db,$q1);
test($db,$q2);

如果不在函数内部使用 mysqli 链接,我将永远不会看到查询 #2 返回。我唯一得到的是第二个查询的错误。 (multi_query(): 无法获取 mysqli)


I just ran your code and here is what I get from the error log 

[2010 年 1 月 8 日星期五 08:12:43] [错误] [客户端 127.0.0.1] PHP 警告:mysqli::multi_query():无法在 C:\Program Files\Apache Software Foundation\Apache2 中获取 mysqli .2\htdocs\test\1.php 第 24 行

[2010 年 1 月 8 日星期五 08:12:43] [错误] [客户端 127.0.0.1] PHP 警告:mysqli::close():无法在 C:\Program Files\Apache Software Foundation\Apache2 中获取 mysqli .2\htdocs\test\1.php 第 38 行 [2010 年 1 月 8 日星期五 08:12:43]

[错误] [客户端 127.0.0.1] PHP 警告:mysqli::multi_query():无法在 C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\test\1.php 中获取 mysqli第 24 行 [2010 年 1 月 8 日星期五 08:12:43]

[错误] [客户端 127.0.0.1] PHP 警告:mysqli::close():无法在 C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\test\1.php 中获取 mysqli第 38 行 [2010 年 1 月 8 日星期五 08:12:43]

[错误] [客户端 127.0.0.1] PHP 警告:mysqli::multi_query():无法在 C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\test\1.php 中获取 mysqli第 24 行 [2010 年 1 月 8 日星期五 08:12:43]

[错误] [客户端 127.0.0.1] PHP 警告:mysqli::close():无法在 C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\test\1.php 中获取 mysqli第 38 行 [2010 年 1 月 8 日星期五 08:12:43]

[错误] [客户端 127.0.0.1] PHP 警告:mysqli::multi_query():无法在 C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\test\1.php 中获取 mysqli第 24 行 [2010 年 1 月 8 日星期五 08:12:43]

[错误] [客户端 127.0.0.1] PHP 警告:mysqli::close():无法在 C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\test\1.php 中获取 mysqli第 38 行


这是我现在正在运行的,但仍然只返回一个查询,恰好是第一个列出的“注释”

function query_db( $link, $query ) {

 /* execute multi query */
 if ( $link->multi_query( $query ) ) {
    do {
        /* store first result set */
        if ($result = $link->store_result() ) {
      while( $row =  $result->fetch_row() ) {
       print_r( $row );
      }
      echo '<br/>';
               $result->close();
        }
     if( $link->more_results() ) {
     // ...
     }
    } while ( $link->next_result() );
 }
}

$db = new mysqli( 'xxxx', 'xxxx', 'xxxx', 'xxxx' );
query_db( $db, 'CALL Notes();'
query_db( $db, 'CALL Users();'
query_db( $db, 'SELECT * FROM test';

$db = new mysqli('xxxx','xxxx','xxxx','xxxx');

$f1 = fa($db,"CALL phones();");
$f2 = fa($db,"CALL users();");

好的,非常简单.. 我们查询数据库 2 次。一旦获得所有电话,然后将返回集分配给 var $f1。我们对 $f2 做同样的事情。当我运行这段代码时,$f2 从来没有结果。它总是空的。但是,当我在每次调用时独立执行 print_r 时,我会得到正确的结果。这真的很奇怪!您应该能够使用我们在下面发布的代码复制这种完全相同的行为。只需尝试将返回的集合分配给每个变量,然后尝试回显结果。

最佳答案

连接在第一次调用此函数后关闭,这就是您无法从第二次调用中获得结果的原因。

此外,如果您不一次传入两个或多个查询,为什么还要使用 multi_query? 您有两个选择:

您可以连接两个 SQL 字符串,并使用连接在一起的查询调用函数 ONCE。这就是我们在这里使用 multi_query 的原因。

或者(这是我喜欢做的):

删除 $link->close(); 行并根据需要多次调用该函数。 ;)

这样,该函数可以充当 querymulti_query 的包装函数,因为您可以传入任意数量的查询,但也也只有一个查询。

当使用存储过程时,next_result() 方法返回函数的返回值,然后如果你再次调用它,它返回实际的结果集,这就是为什么我有一个名为 $sp

这是更新后的代码:

$db = new mysqli( 'xxxx', 'xxxx', 'xxxx', 'xxxx' );

function query_db( $link, $query, $sp = false ) {
 $set_array = array();
 /* execute multi query */
 if ( $link->multi_query( $query ) ) {
    do {
        /* store result set */
        if ($result = $link->store_result() ) {
            $set_array[] = $result;
        }
        if( $link->more_results() ) {
            // ...
        }
    } while ( $link->next_result() && ( $sp ? $link->next_result() : true ) );
 }
 if( count( $set_array ) ) 
     return $set_array;
 return false;
}

$table_set = query_db( $db, 'show tables;');
$notes_set = query_db( $db, 'CALL Notes();', true ); // stored procedure.
$users_set = query_db( $db, 'CALL Users();', true ); // stored procedure.

用法:

foreach( $users_set as $set ) {
    while( $row = $set->fetch_row() ) {
        print_r( $row );
        echo '<br/>';
    }
}

关于php - mysqli 链接正在掉线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2028434/

相关文章:

php - 在 Mysqli 准备语句中返回一个数组

php - 找不到存储库方法

php - 如何检索表的列数据,将其存储到数组中,最后回显该数组

mysql - sql : add euro sign and round the number

mysql - 默认 MySQL 数据库名称

Mysql使用PDO的错误信息

php - 如何从 html 选项中检索 php 变量中分隔的整个字符串空间?

php - 错误: `Trying to get property of non-object` in PHP upload from AngularJS

php - 寻找一个好的数据库结构来实现类似 Facebook/SO 的通知

php - 如果另一个执行 php 脚本,则阻止执行该脚本