用户函数中的 php data_seek

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

然后我调用这个函数两次,只得到一个(第一个)结果:

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

我使用了 $result->data_seek(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;
}

关于用户函数中的 php data_seek,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30624856/

相关文章:

php - 检查并检索数据库列日期是否在接下来的 X 天内

MySQL 相当于 MS SQL 的 TRIGGER_NESTLEVEL()?

php - 连接其他表中的一列并回显它

mysql - SQL 仅选择列上具有最大值的行

带有匿名连接的 PHP mysqli 准备语句

php - 非静态方法 mysqli::init() 不能静态调用

php - 我们可以使用 PHP 中的报告 API 从谷歌分析中获取所有用户和 session 吗

javascript - 通过ajax调用删除表中的tr

php - OOP 设计 - 您在哪里/何时验证属性?

mysql - 如何降级 Wamp MySQL 版本?