0

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?

jasonlcy
  • 3
  • 2

3 Answers3

1
$x=1;
foreach ($sth->fetchall() as $row){ 
      echo '<br /> This is an iteration ',$x;
      $results = $row[$tablefields];
      echo $results; 
      $x++;
}

Instead of using for loop and foreach together you can use the above code.

Sameer Shaikh
  • 6,196
  • 2
  • 13
  • 31
1

Here is the thing..

I don't know why you are executing the query thrice or each time loop is running. But here is the little improvement over your code. Take a look.

$query = "SELECT $tablefields FROM $tablename";
$sth = $dbconn->prepare($query);
$sth->execute();
$data = "";
foreach ($sth->fetchall() as $row) {
   $data .= $row[$tablefields] . "<br>";
}

for ($x = 1; $x <=3; $x++){
   echo '<br /> This is an iteration ' . $x;
   echo $data;
}
abhij89
  • 595
  • 2
  • 18
Coder anonymous
  • 923
  • 1
  • 8
  • 24
  • Thanks! Basically same as the answer above, just a little bit behind in posting =) Would like to know your reason as well for using the dot operator instead of a comma function to join echo strings. Is it a standard practice to use the dot operator instead? – jasonlcy May 14 '15 at 12:47
  • here lies your answer..http://stackoverflow.com/questions/1466408/difference-between-and-in-php – Coder anonymous May 14 '15 at 13:11
  • Got it. Both dot and comma outputs the same thing in echo but really depends on how you structure it. – jasonlcy May 14 '15 at 13:36
0

Because you do MySQL query 20 times. You can cache the result and then loop using the cache. For example:

$query = "SELECT $tablefields FROM $tablename";
$sth = $dbconn->prepare($query);
$sth->execute();

$result = "";
foreach ($sth->fetchall() as $row) {
    $result .= $row[$tablefields] . "<br>";
}

for ($x = 1; $x <=3; $x++){
    echo '<br /> This is an iteration ' . $x;
    echo $result;
}
invisal
  • 10,610
  • 3
  • 29
  • 53
  • Works great! I didn't know about the ".=" operator....something new I learned today =) BTW, is there a specific reason the concatenate operator "." (dot) is used over the "," (comma) when joining strings? I read that the comma function is faster over the dot operator when parsing huge amounts of text. – jasonlcy May 14 '15 at 12:46