2015-04-01 78 views
-1

類PHPPHP PDO接力取數據不工作

<?php 

    class product extends db { 

     function viewCat(){ 

      $dbcon = new db(); 
      $connn = $dbcon->dbcon(); 

      try { 
       $stmt = $connn->prepare("SELECT * FROM `cat`"); 

       $resultcat = $stmt->execute(); 

       return $resultcat; 

      } catch (PDOException $e) { 
       echo 'Error: ' . $e->getMessage(); 
      } 

     } 

    } 

    ?> 

視圖

<?php 
$menu = new product(); 

$resultmenux = $menu->viewCat(); 


    foreach ($resultcatx as $row) { 
     print_r($row); 
    } 

?> 

我得到的誤差是

警告:的foreach()

供給參數無效
+0

你只是執行查詢的回答你是不是獲取數據

$stmt = $connn->prepare("SELECT * FROM 'cat'"); $stmt->execute(); $resultcat = $stml->fetchAll(PDO::FETCH_ASSOC); // this line was missing return $resultcat; 

,並在視圖文件。 '你不提取數據'.... – 2015-04-01 06:04:16

回答

1
在類文件應該是

,因爲我的評論說:shankhan

$resultmenux = $menu->viewCat();  

foreach ($resultmenux as $row) { 
    print_r($row); 
} 
1

應該是這樣的:

$resultmenux = $menu->viewCat(); 


foreach ($resultmenux as $row) { 
    print_r($row); 
}