用户函数中的php data_seek

时间:2015-06-03 15:46:26

标签: php mysql mysqli

我与mysql&需要多次获取查询结果。 我用:

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

然后查询:

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

我在单独的文件中有函数,它给出了标题名称:

    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;
    }

然后我调用此函数两次,并且只得到1(第一)结果:

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

我使用了$ result-> data_seek(0);重置指针......但它不起作用。如果我在文件中使用功能代码两次 - 那么它的工作原理。 你知道为什么吗? 干杯!

1 个答案:

答案 0 :(得分:0)

你可以让这个功能更加简单

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;
}