Are fetchAll() and for() good choices for my code? I use the following code: fetchAll and for($mich=0;$mich&lt;10;$mich++).

Is this the best way?

Is there a better way, either to optimize or replace this code?

&lt;?php
include("cache/phpfastcache.php");
phpFastCache::setup("storage","auto");
$cache = phpFastCache();
$products = $cache-&gt;get("product_page");
if($products == null) {
    $akhbarkotah1 = $conn-&gt;prepare("SELECT text,time FROM small WHERE active='0' ORDER BY time DESC LIMIT 10");
    $akhbarkotah1-&gt;execute();
    $products = $akhbarkotah1-&gt;fetchAll();
    $cache-&gt;set("product_page", $products,30);
}
for($mich=0;$mich&lt;10;$mich++){
    $text1 =$products[$mich][0];
    $time1 =$products[$mich][1];
    ?&gt;
    &lt;a class="lastnews"&gt;
        &lt;div id="lastnews_title" class="YekanBlack10" style="width:585px;"&gt;
            &lt;div&gt;&lt;?php echo $text1; ?&gt;&lt;/div&gt;
            &lt;div class="YekanRed10" style="text-align:left"&gt;
                &lt;?php echo timeTonow($time1); ?&gt;
            &lt;/div&gt;
            &lt;div style="border-bottom:2px solid #7c0000;height:5px;"&gt;&lt;/div&gt;
        &lt;/div&gt;
    &lt;/a&gt;
    &lt;?php
}
?&gt;
Using fetchAll usually is not a problem. There are some things to optimize here in my opinion:


Use a foreach loop instead of for loop. Your result is already limited. No need to duplicate this here. This removes the iterator counter and assignment:

foreach ($products as $product) {
    $text1 = $product[0];
    $time1 = $product[1];
?&gt;

Using the Object fetch mode (returns objects instead of indexed arrays) makes this even more readable and saves you the name assignments:

foreach ($products as $product) {
     // $product-&gt;text1; $product-&gt;time1;
?&gt;

Though I'd recommend to remove these. Introducing numbers in variable names usually suggests there is a second one. E.g. time2.
As @mat's mug pointed out, indentation is very important. Same goes for empty lines. There are great to structure your code in logical blocks. For this example here, I'd have an empty line after setting up the cache and one after loading the products.