I am trying to iterate through all users and call a function on each username from an accounts table I have on a MySQL database.
At first, I tried:
$result = mysqli_query($mysqli, "SELECT username FROM accounts");
while ($row = $result->fetch_assoc()) {
try {
getFollowerData($row["username"], $mysqli, $ig);
} catch (Exception $e) {
echo "$e \n";
}
}
But that used too much memory as I have over 4 million records in the table.
So I then tried:
$result = mysqli_query($mysqli, "SELECT username FROM accounts", MYSQLI_USE_RESULT);
while ($row = $result->fetch_assoc()) {
try {
getFollowerData($row["username"], $mysqli, $ig);
} catch (Exception $e) {
echo "$e \n";
}
}
However, when I do this I get:
error: Commands out of sync; you can't run this command now
because I have the line:
$result = mysqli_query($mysqli, $insertFollowerData);
in my getFollowerData() function, meaning I'm trying to make an SQL query while I am still making the first query to get all the usernames from accounts.
Is there any way to make these queries simultaneously?