title: SQL query in php webpage runs very slow in displaying 30,000 entries
body: <p>I have a database with about 30,000 rows that I would like to display in a webpage. Unfortunately it takes a very long time to fetch the data and freezes the browser multiple times during this operation. How can I speed this up?</p>
  
  <pre><code>&lt;?php require_once'includes/session.php'; ?&gt;
  &lt;?php require_once'includes/db_connect.php'; ?&gt;
  &lt;?php
  $page = $_SERVER['PHP_SELF'];
  $sec = "50";
  ?&gt;
  
  
      &lt;!DOCTYPE html&gt;
      &lt;html&gt;
      &lt;head&gt;
  
       &lt;meta http-equiv="refresh" content="&lt;?php echo $sec?&gt;;URL='&lt;?php echo $page?&gt;'"&gt;
  
      &lt;title&gt; Real Time Eelection Results&lt;/title&gt;
  
  
           &lt;link rel="stylesheet" type="text/css" href="css/css-table.css"&gt;
  
           &lt;script type="text/javascript" src="js/jquery-1.2.6.min.js"&gt;&lt;/script&gt;
            &lt;script type="text/javascript" src="js/style-table.js"&gt;&lt;/script&gt;
  
          &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
  &lt;/head&gt;
  
  &lt;/head&gt;
  
  
  &lt;body&gt;
  
  
                  &lt;form action="search.php" method="GET"&gt;
                  &lt;input type="text" name="sql" /&gt;
                   &lt;input type="submit" value="Search" /&gt;
                  &lt;/form&gt;
  
  
  
  
  
                      &lt;?php 
  
                         $sql = "SELECT * FROM data_db"
                         ;
  
  
                        if($result = mysqli_query($connection, $sql)){
                           if(mysqli_num_rows($result) &gt; 0){
  
  
                             echo "&lt;table&gt;";
  
                                  echo "&lt;thead&gt;";
  
                                  echo "&lt;tr&gt;";
                                  echo "&lt;th&gt;ID&lt;/th&gt;";
                                  echo "&lt;th&gt;DATE&lt;/th&gt;";
                                  echo "&lt;th&gt;Data 1    &lt;/th&gt;";
                                    echo "&lt;th&gt;Data 2  &lt;/th&gt;";
                                      echo "&lt;th&gt;Data 3&lt;/th&gt;";
                                        echo "&lt;th&gt;Data 4  &lt;/th&gt;";
  
                                  echo "&lt;th&gt;Data 5&lt;/th&gt;";
                                  echo "&lt;th&gt;Data 6&lt;/th&gt;";
                                  echo "&lt;th&gt;Data 7 &lt;/th&gt;";
                                  echo "&lt;th&gt;Data 8&lt;/th&gt;";
                                  echo "&lt;th&gt;Data 9&lt;/th&gt;";
                                  echo "&lt;th&gt;Data 10&lt;/th&gt;";
                                  echo "&lt;th&gt;Data 11&lt;/th&gt;";
                                  echo "&lt;th&gt;Data 12&lt;/th&gt;";
                                  echo "&lt;th&gt;Data 13&lt;/th&gt;";
                                  echo "&lt;th&gt;Data 14&lt;/th&gt;";
                                  echo "&lt;th&gt;Data 15&lt;/th&gt;";
                                  echo "&lt;th&gt;Data 16&lt;/th&gt;";
                                  echo "&lt;th&gt;Data 17&lt;/th&gt;";
                                  echo "&lt;th&gt;Data 18&lt;/th&gt;";
                                  echo "&lt;th&gt;Data 19&lt;/th&gt;";
                                  echo "&lt;th&gt;Data 20&lt;/th&gt;";
                                  echo "&lt;th&gt;Data 21&lt;/th&gt;";
                                  echo "&lt;/tr&gt;";
  
                                    echo "&lt;/thead&gt;";
  
                              while($row = mysqli_fetch_array($result)){
                                echo "&lt;tr&gt;";
                                echo "&lt;td&gt;&lt;b&gt;" . $row['id'] . "&lt;/td&gt;";
                                echo "&lt;td&gt;&lt;b&gt;" . $row['date'] . "&lt;/td&gt;";
  
                                  echo "&lt;td&gt;&lt;b&gt;" . $row['data_1'] . "&lt;/td&gt;";
                                  echo "&lt;td&gt;&lt;b&gt;" . $row['data_2'] . "&lt;/td&gt;";
                                  echo "&lt;td&gt;&lt;b&gt;" . $row['data_3'] . "&lt;/td&gt;";
                                  echo "&lt;td&gt;&lt;b&gt;" . $row['data_4'] . "&lt;/td&gt;";
                                  echo "&lt;td&gt;&lt;b&gt;" . $row['data_5'] . "&lt;/td&gt;";
                                  echo "&lt;td&gt;&lt;b&gt;" . $row['data_6'] . "&lt;/td&gt;";
                                  echo "&lt;td&gt;&lt;b&gt;" . $row['data_7'] . "&lt;/td&gt;";
                                   echo "&lt;td&gt;&lt;b&gt;" . $row['data_8'] . "&lt;/td&gt;";
                                  echo "&lt;td&gt;&lt;b&gt;" . $row['data_9'] . "&lt;/td&gt;";
                                  echo "&lt;td&gt;&lt;b&gt;" . $row['data_10'] . "&lt;/td&gt;";
                                   echo "&lt;td&gt;&lt;b&gt;" . $row['data_11'] . "&lt;/td&gt;";
                                  echo "&lt;td&gt;&lt;b&gt;" . $row['data_12'] . "&lt;/td&gt;";
                                  echo "&lt;td&gt;&lt;b&gt;" . $row['data_13'] . "&lt;/td&gt;";
                                    echo "&lt;td&gt;&lt;b&gt;" . $row['data_14'] . "&lt;/td&gt;";
                                    echo "&lt;td&gt;&lt;b&gt;" . $row['data_15'] . "&lt;/td&gt;";
                                    echo "&lt;td&gt;&lt;b&gt;" . $row['data_16'] . "&lt;/td&gt;";
                                    echo "&lt;td&gt;&lt;b&gt;" . $row['data_17'] . "&lt;/td&gt;";
                                    echo "&lt;td&gt;&lt;b&gt;" . $row['data_18'] . "&lt;/td&gt;";
                                    echo "&lt;td&gt;&lt;b&gt;" . $row['data_19'] . "&lt;/td&gt;";
                                    echo "&lt;td&gt;&lt;b&gt;" . $row['data_20'] . "&lt;/td&gt;";
  
                                    $pink = $row['data_21'];
  
                 echo "&lt;td&gt;&lt;b&gt;&lt;a href =".$pink.'.jpg'."&gt;Image&lt;/a&gt;&lt;/td&gt;";
  
  
                                echo "&lt;/tr&gt;";
                                }
  
                                echo "&lt;/table&gt;";
  
  
                              // Close result set
                              mysqli_free_result($result);
                                    } else{
                               echo "No records matching your query were found.";
                              }
                              } else{
                              echo "ERROR: Could not able to execute $sql. " . mysqli_error($connection);
                                  }
  
                              // Close connection
                              mysqli_close($connection);
  
                              ?&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>