0

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";
}
3
  • what is $connection$j and prodDB$i(); it is syntax error Commented Nov 3, 2013 at 5:34
  • @IlyaBursov Hence my problem. I have 35 functions set up, each connecting to a different database. I want this loop to connect to each of the databases 1 - 35, using its respective function and returning the unique connection string. Commented Nov 3, 2013 at 6:00
  • try $q = 'prodDB'.$i;print_r($q()); Commented Nov 3, 2013 at 6:02

1 Answer 1

1

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

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.