php pdo oop获取数据不起作用

时间:2015-04-01 05:59:55

标签: php oop pdo

PHP类

<?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()提供的参数无效

2 个答案:

答案 0 :(得分:1)

应该是这样的:

$resultmenux = $menu->viewCat();


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

答案 1 :(得分:1)

应该是你的类文件中的

,因为我评论说你没有提取数据

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

$stmt->execute();

$resultcat = $stml->fetchAll(PDO::FETCH_ASSOC); // this line was missing

return $resultcat;

并在视图文件中由shankhan回答

$resultmenux = $menu->viewCat();    

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