使用mysqli_fetch_array多次遍历查询结果? [英] Looping through query results multiple times with mysqli_fetch_array?

查看:555
本文介绍了使用mysqli_fetch_array多次遍历查询结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

mysqli_fetch_array()函数是否在返回每个值时将其删除?如果没有,完成循环遍历后如何返回数组顶部?

Does the mysqli_fetch_array() function remove each value as it returns it? If not, how can I go back to the top of the array when I've finished looping through it?

我需要遍历该列表几次,因为我正在使用它来生成唯一的用户名(如果已经使用,请在末尾添加数字).

I need to loop through the list several times, as I'm using it to generate unique usernames (by adding numbers to the end if it's already taken).

$uniqueName = true;

while($row = mysqli_fetch_array($namesList)) {
    if ($row['Username'] == $UserBase) {
        $uniqueName = false;
    }
}

$number = 0;
if ($uniqueName == true) {
    $User = $UserBase;
}
else {
    while ($uniqueName == false) {
        $uniqueName = true;
        $number++;
        $tempList = $namesList2;
        while($row = mysqli_fetch_array($tempList)) {
            if ($row['Username'] == $UserBase . $number) {
                $uniqueName = false;
            }
        }
    }
    $User = $UserBase . $number;
}

$namesList是到目前为止数据库中所有用户名的列表.基本上,$UserBase是加在一起的用户的姓氏和姓氏.当前发生的情况是$User变为$Userbase,最后是1,即使应为2.但是,如果它是名称的第一个实例,则不会添加任何内容(即按预期工作).

$namesList is a list of all usernames in the database so far. Basically, $UserBase is the forename and surname of the user added together. What happens at the moment is that $User becomes $Userbase with a 1 on the end, even when it should be 2. However, if it's the first instance of the name then it doesn't add anything (which is working as intended).

推荐答案

魔术

$data = array();
while ($row = mysqli_fetch_array($res))
{
    $data[] = $row;
}

foreach ($data as $row) echo $row['name'];
foreach ($data as $row) echo $row['name'];
foreach ($data as $row) echo $row['name'];
foreach ($data as $row) echo $row['name'];
foreach ($data as $row) echo $row['name'];
foreach ($data as $row) echo $row['name'];

这篇关于使用mysqli_fetch_array多次遍历查询结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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