 <p>First: add status as an index on the database table, this will speed up the select portion of the script.</p> <p>Second: concatenate the string, rather than issuing an echo statement each time, echo * 4000 can be quite slow.</p> <pre><code>$query = "SELECT * FROM tblRazduzeniUgovori WHERE Status='razduzen'"; $output = ''; if ($result = mysqli_query($con, $query)) { while($row = mysqli_fetch_array($result)) { $output .= "&lt;tr&gt;"; $output .= "&lt;td&gt;&lt;input type='checkbox' name='brojUgovora[]' value='" . $row['Broj'] . "'/&gt;&lt;/td&gt;"; } } echo $output; </code></pre>