用户函数中的php data_seek

I have connection with mysql & need to get query results many times. I use:

$mysqli = new mysqli(DB_SERVER, DB_USER, DB_PASS, DB_NAME);

Then query:

$query = "CALL raport6('2014-01-01', '2014-12-31', 300);";
$result = $mysqli->query($query);

I have function in separate file which gives me header names:

    function headers($result) {
        global $result;

        $field_cnt = $result->field_count;
        $string ="";

        while ($finfo = $result->fetch_field()) {
            $currentfield = $result->current_field;
            $string .= "'" . $finfo->name . "'";
            if ($currentfield == $field_cnt) {break;}
            $string .= ",";
        }
        $result->data_seek(0);
        return $string;
    }

Then I call this function twice, and get only 1 (first) result:

echo "function 1:" . headers($result);
echo "function 2:" . headers($result);

I used $result->data_seek(0); to reset pointer... but it doesn't work in function. If I use function code in file twice - then it works. Do you know why? Cheers!

You could make that function a whole lot simpler

function headers($result) {

    $string = '';

    while ($finfo = $result->fetch_field()) {
        $string .= sprintf("'%s',", $finfo->name);
    }
     // and here is the fix
    $result->field_seek(0);

    // to remove last comma if required
    // return rtrim($string, ',');

    return $string;
}