为什么使用mysqli_fetch_assoc无法两次显示相同的结果? [英] Why I can't display same result twice using mysqli_fetch_assoc?

查看:119
本文介绍了为什么使用mysqli_fetch_assoc无法两次显示相同的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的页面之一下显示了一些结果.但是,当我试图在同一页面下两次获得相同的结果时,它并没有以所需的方式显示.我已经包含了功能文件,并首先调用了该功能.然后,我尝试两次显示相同的结果.但是只有第一个结果显示,而第二个则没有.这是代码:

I am displaying some results under one of my pages. But while I am trying to get the same result two times under the same page, it is not showing in the desired way. I have included the function file and called the function at first. Then I tried to display the same result twice. But only the first result is displaying, second one is not. Here's the code:

require 'functions.php';
$query_result=select_all_published_category();

<div class="control-group">
        <label class="control-label">Parent Category Name</label>
        <div class="controls">
            <select name="category_id">

                <?php while($cat_info=mysqli_fetch_assoc($query_result)) {?>
                <option value="<?php echo $cat_info['category_id']; ?>"><?php echo $cat_info['category_name']; ?></option>
                <?php }?>

            </select>
        </div>
</div>

<div class="control-group">
        <label class="control-label">Parent Category Name 2</label>
        <div class="controls">
            <select name="category_id2">

                <?php while($cat_info_two=mysqli_fetch_assoc($query_result)) {?>
                <option value="<?php echo $cat_info_two['category_id']; ?>"><?php echo $cat_info_two['category_name']; ?></option>
                <?php }?>

            </select>
        </div>
</div>

有人可以帮我吗?谢谢

推荐答案

根据 mysqli_fetch_assoc手册

返回代表所获取行的字符串关联数组 在结果集中,其中数组中的每个键代表 结果集的列之一;如果其中没有更多行,则为NULL 结果集.

Returns an associative array of strings representing the fetched row in the result set, where each key in the array represents the name of one of the result set's columns or NULL if there are no more rows in resultset.

因此,在第一次迭代之后(使用while)它为空.因此,您不能直接进行第二次迭代.

So after first iteration(using while) it's get empty.So you can't do second-iteration directly.

解决方案:首先创建一个变量,然后将所有数据分配给该变量.现在,您可以根据需要多次使用它

Solution: Create a variable first, and assign all data to that variable.Now use it as many time as you want

<?php
require 'functions.php';
$query_result=select_all_published_category();
$category = []; //create array
while($cat_info=mysqli_fetch_assoc($query_result)) {
    $category[] = $cat_info; //assign whole values to array
}
?>

<div class="control-group">
        <label class="control-label">Parent Category Name</label>
        <div class="controls">
            <select name="category_id">

                <?php foreach($category as $cat){?>
                    <option value="<?php echo $cat['category_id']; ?>"><?php echo $cat['category_name']; ?></option>
                <?php }?>

            </select>
        </div>
</div>

<div class="control-group">
        <label class="control-label">Parent Category Name 2</label>
        <div class="controls">
            <select name="category_id2">
                <?php foreach($category as $cat){?>
                    <option value="<?php echo $cat['category_id']; ?>"><?php echo $cat['category_name']; ?></option>
                <?php }?>
            </select>
        </div>
</div> 

这篇关于为什么使用mysqli_fetch_assoc无法两次显示相同的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆