将MYSQL迁移到MYSQLi:输出缓冲区处理程序中的DB函数无法正常工作

I am migrating a legacy project from mysql_ to mysqli_.

Somewhere in the code there is

ob_start("do_output"); 

And in my

function do_output($output) { 
     [...]
     querydb(' .... ');
     [...]
} 

where querydb() looks like thi

function querydb($query,$dieonerrors=true,$ec=false) {
    global $db_conn, $db_type, $debuginfos, $stat_qcount;
    ...

Now when I check this with the debugger $db_conn is null. It was initialized with $db_conn = mysqli_connect() earlier. And the connection was working during the run. But inside of the output buffer handler it became null. How can this be? With mysql_*it was working.

EDIT Here is the full code demonstrating the problem:

<?php

$db_type = "mysqli";
$db_host = "localhost";
$db_database = "test";
$db_user = "root";
$db_pass = "root";

initdb();

querydb("SHOW TABLES");
ob_start('do_output');
querydb("SHOW TABLES");

echo "foobar";

function initdb($persistent = false)
{
    global $db_host, $db_user, $db_pass, $db_database, $db_type, $db_conn;
    $db_conn = mysqli_connect($db_host, $db_user, $db_pass);
    mysqli_query($db_conn, "USE $db_database");
    return $db_conn;
}


function querydb($query, $dieonerrors = true, $ec = false)
{
    global $db_conn, $db_type, $debuginfos, $stat_qcount;
    $result = mysqli_query($db_conn, $query);
    if (!$result) {
        if ($dieonerrors) {
            die('error');
        }
    }
    return $result;
}


function do_output($output)
{
    querydb('SHOW TABLES');  // if I comment this I get an output 'foo'
    return "foo";
}

I get no output and the error in /var/log/apache2/error.log is

PHP Warning:  mysqli_query() expects parameter 1 to be mysqli, null given in /home/foo/bar/test.php on line 32