3

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 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).

0

1 Answer 1

6

You could use the mysqli_fetch_all function to return all results as an array, which you could then manipulate as you would any other array in PHP:

$arr = mysqli_fetch_all($namesList, MYSQLI_ASSOC);

note that by default mysqli_fetch_all() returns enumerated rows, as in mysqli_fetch_row(), and if you need associative arrays, you need to add MYSQLI_ASSOC as a parameter.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.