I've been trying to learn PHP for the past week and I'm stuck at a particular problem. Please excuse my poorly worded title as I haven't grasped the jargon yet.
Here's my code:
$query = "SELECT $tablefields FROM $tablename";
$sth = $dbconn->prepare($query);
for ($x = 1; $x <=3; $x++){
echo '<br /> This is an iteration ',$x;
$sth->execute();
foreach ($sth->fetchall() as $row)
{
$results = $row[$tablefields];
echo $results, '<br />'
;
}
}
I want the code to output as below:
This is iteration No. 1
Apple
Orange
Banana
Kiwi
This is iteration No. 2
Apple
Orange
Banana
Kiwi
This is iteration No. 3
Apple
Orange
Banana
Kiwi
Now, the above code does the job just fine but when I want to increase the iterations $x<=20, and the fruit options to 20 items, there's a noticeable slowdown in the page rendering. I was wondering whether it's because the code is querying the MySQL database 20 times? Is there a better way to structure the code so that it's more efficient?