首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >PHP mysqli结果对一个函数有效一次,但不能对另一个函数第二次有效

PHP mysqli结果对一个函数有效一次,但不能对另一个函数第二次有效
EN

Stack Overflow用户
提问于 2018-06-08 06:58:55
回答 1查看 54关注 0票数 0

我有一个php页面,在那里我从一组SQL查询中生成了一个json文件。然后将结果发送到一个构建数组的函数(我知道person_checker和status_indicator是可以工作的函数)。

function array_builder($result, $id_name, $column_name, $type, $color)
    {
        global $mysqli;

        while ($row = $result->fetch_array())
        {

            $type_construct = $type . $row[$id_name];

            switch ($type)
                {
                case "work":
                    person_checker($row);
                    $url_color = status_indicator($row,$type)[0];
                    $link = status_indicator($row,$type)[1];
                    $new_type = $type;
                    $new_color = $color;
                    break;

                case "other_work":
                    person_checker($row);
                    $url_color = status_indicator($row,$type)[0];
                    $link = status_indicator($row,$type)[1];
                    $new_type = $type;
                    $new_color = $color;
                    break;

                case "person":
                    $url_color = status_indicator($row,$type)[0];
                    $link = status_indicator($row,$type)[1];
                    $new_type = $type;
                    $new_color = $color;
                    break;

                default:
                    $url_color = 'aliceblue';
                    $link = NULL;
                    $new_color = $color;
                    $new_type = $type;
                }

                $item_array= array(
                'id' => $type_construct,
                'name' => $name,
                'type' => $new_type,
                'color' => $new_color,
                'url_color' => $url_color,
                'link' => $link,
                );

                array_push($GLOBALS['array'], $item_array);

         }
}   

这一切都运行得很好。但是,在构建数组以生成数组中的项之间的链接之后,我需要再次运行结果,而我的函数不会生成任何结果:

function link_builder($result,$source_type,$target_type,$source_id,$target_id) {

    global $mysqli;
    global $person_color;
    global $multiple_author_color;
    global $otherwork_other_color;
    global $otherwork_lydgate_color;

    while ($row = $result->fetch_array()) {

    var_dump($result);
    echo"<br>";

    $source_location = array_search_multidim($GLOBALS['array'],'id',$source_type . $row[$source_id]);

    $target_location = array_search_multidim($GLOBALS['array'],'id',$target_type . $row[$target_id]);

    $color = $GLOBALS['array'][$target_location]['color'];

        $array = array(
        'color' => $color,
        'source' => $GLOBALS['array'][$source_location]['id'],
        'target' => $GLOBALS['array'][$target_location]['id'],
        'value' => 1);

        if (in_array_r($array,$GLOBALS['link_array']))
        {}
        else
        {
            array_push($GLOBALS['link_array'],$array);
        }
    }
}

我想这可能是我写的函数之一,但即使我将所有内容都剥离到只有var_dump,我仍然得不到任何结果。这个对象仍然存在,因为我可以在页面的开头和结尾转储它,并在这两个地方得到相同的结果:object(mysqli_result)#2 (5) { ["current_field"]=> int(0) ["field_count"]=> int(3) ["lengths"]=> NULL ["num_rows"]=> int(8) ["type"]=> int(0) }我可以看到其他人也遇到过这个问题,但建议的解决方案都不起作用。是我做错了什么,还是mysqli出了什么我不知道的事情?我不想进行另一组SQL调用,这就是我当前版本的页面所做的事情。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-08 07:13:45

一旦运行了第一个函数,就遍历了整个结果集。当您运行第二个函数并传入相同的mysqli_result对象时,其内部指针已设置为末尾,因此调用$result->fetch_array()将不会产生(更多)结果。

幸运的是,修复方法很简单:您需要在函数调用之间倒回结果集,以便可以再次遍历结果集:

array_builder($result, $id_name, $column_name, $type, $color);
$result->data_seek(0);
link_builder($result, $source_type, $target_type, $source_id, $target_id);

请注意the manual中的以下内容

备注:

此函数只能与通过使用mysqli_store_result()mysqli_query()函数获得的缓冲结果一起使用。

如果您的结果来自预准备语句,并且您通过调用$stmt->get_result()来获取$result,则需要将其更改为使用$stmt->store_result()以获取缓冲的结果集而不是常规的结果集,并且(可选的,除非您的应用程序在服务器的内存限制附近运行,否则不应该是必需的)之后调用$stmt->free_result()来释放缓冲的结果所使用的内存。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50751228

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档