Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>These are some issues in your code that affects performance:</p> <ol> <li><p><strong>You should not call <code>print</code> for each row of your table</strong>. That penalizes performance (if the server is not caching the output) as everytime you call <code>print</code> you will be sending bytes across the net which is a costly operation that is better be done <strong>once for one big chunk of data</strong> rather than <strong>many times for small chunks of data</strong> , that is the reason web servers will often cache all your PHP output prior to sending it to the browser.</p></li> <li><p>You should <strong>pass by reference the array value</strong> when traversing the array with foreach, to avoid the copy of a variable in each iteration of the loop.</p></li> <li><p><strong>Echo with commas, not periods</strong>. If you use periods, PHP has to concatenate the string before it outputs. If you use commas, it just outputs them in order with no extra processing.</p></li> <li><p><strong>You should use echo instead of <code>print()</code></strong>. As a language construct rather than a function, echo has a slight performance advantage over print().</p></li> </ol> <p>So this is your code with points 2, 3 and 4 above corrected, thus assuming your web server is caching output:</p> <pre><code>&lt;?php $query = $db-&gt;query('SELECT type FROM card_type'); $rows = $query-&gt;fetchAll(); foreach($rows as &amp;$row) { echo '&lt;option value="', $row['type'] ,'"&gt;' ,$row['type'] , '&lt;/option&gt;'; } ?&gt; </code></pre> <p>and this is your code with point 1 and 2 above corrected, thus assuming your web server is not caching the output:</p> <pre><code>&lt;?php $query = $db-&gt;query('SELECT type FROM card_type'); $rows = $query-&gt;fetchAll(); $out = ''; foreach($rows as &amp;$row) { $out .= '&lt;option value="'.$row['type'].'"&gt;'.$row['type'].'&lt;/option&gt;'; } echo $out; ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload