使用带有foreach的Object fetch模式

时间:2014-03-04 15:24:50

标签: php pdo

我在代码中使用$text =$product[0];$time =$product[1];

<?php
include("cache/phpfastcache.php");
phpFastCache::setup("storage","auto");
$cache = phpFastCache();
$products = $cache->get("product_page");
if($products == null) {
$akhbarkotah1 = $conn->prepare("SELECT text,time FROM small WHERE active='0' ORDER BY time DESC LIMIT 10");
$akhbarkotah1->execute();
$products = $akhbarkotah1->fetchAll();
$cache->set("product_page", $products,10);
}
foreach($products as $product){
$text =$product[0];
$time =$product[1];
?>
<a class="lastnews">
<div id="lastnews_title" class="YekanBlack10" style="width:585px;">
<div><?php echo $text; ?></div>
<div class="YekanRed10" style="text-align:left">
<?php echo timeTonow($time); ?>
</div>
<div style="border-bottom:2px solid #7c0000;height:5px;"></div>
</div>
</a>
<?php
}?>

我希望更改对象获取 何时将变量更改为

foreach($products as $product){
    $text =$product->text;
    $time =$product->time;

无法正常工作

问题出在哪里?

1 个答案:

答案 0 :(得分:0)

更改以下内容:

  $products = $akhbarkotah1->fetchAll();
  $cache->set("product_page", $products,10);
}
foreach($products as $product){
  $text =$product[0];
  $time =$product[1];
?>

  $cache->set("product_page", $products,10);
}
foreach($product = $akhbarkotah1->fetch(PDO::FETCH_OBJ)) {
  $text = $product->text;
  $time = $product->time;
?>