mysqli_fetch_array()
函数在返回每个值时是否删除它?如果不是,我如何在完成循环后回到数组的顶部?
我需要多次遍历该列表,因为我正在使用它来生成唯一的用户名(如果已被使用,则在末尾添加数字)。
$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。但是,如果它是名称的第一个实例,然后它不添加任何内容(按预期工作)。
最佳答案
您可以使用 mysqli_fetch_all 函数将所有结果作为数组返回,然后您可以像操作 PHP 中的任何其他数组一样操作它:
$arr = mysqli_fetch_all($namesList, MYSQLI_ASSOC);
注意默认mysqli_fetch_all()
返回枚举行,如mysqli_fetch_row()
,如果需要关联数组,需要添加MYSQLI_ASSOC
作为参数。
关于php - 使用 mysqli_fetch_array 多次循环查询结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22565844/