Reputation: 490
Is it possible to call functions within for loops? I have a number of databases that are exactly the same, I would like to use a for loop to set up the connections one at a time (35 databases). I am having trouble with getting php to declare a function with a variable in the name.
for ($i = 1; $i <= 35; $i++)
{
$j=$i+2;
echo "<br><br>DB $i";
$connection$j = prodDB$i();
$getFromDB = "select URL, count(id) as counts from ProductList GROUP BY URL";
$NamesReturned = mysqli_query($connection$j,$getFromDB) or die("Can't execute query GFNFDB.<br><br>$getFromDB<br><br>");
while ($ret=mysqli_fetch_array($NamesReturned))
{
$DB$icounts=$ret["counts"];
$URL=$ret["URL"];
echo "<br>$DB$icounts -- $URL";
}
echo "<br>$DB$icounts";
}
Upvotes: 0
Views: 38
Reputation: 133
I think what you need to use is braces. Eg: $connection{$j}=prodDB{$i}();
I could be wrong though. It has been a long time since i did it.
You might be better using arrays and/or passing parameters to your functions. Eg:
$connection[$j]=prodDB($i);
Upvotes: 1