我连接了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/