PHP致命错误:调用非对象上的成员函数

时间:2016-01-25 17:00:00

标签: php

我从表中获取Id,我需要获取新闻项的DB记录,因此管理员可以更新它。它到达控制器就好了,但是当我尝试将它传递给模型并执行查询时,它会给我错误。

让我感到困惑的是,我在整个项目中多次使用完全相同的结构,之前从未遇到过这个错误。

这是控制器:

    include_once'models/Admin_NewsTable.class.php';
    $news = new Admin_NewsTable( $dbh );

    // This one works fine
    $aNews = $news->getAllNews();

    $newsTable = h('');

    foreach($aNews as $news){
        $newsTable .= '<tr>';
        $newsTable .= ' <td>'.$news['title'].'</td>';
        $newsTable .= ' <td>'.substr($news['content'], 0, 50).'</td>';
        $newsTable .= ' <td>'.$news['author_name'].'</td>';
        $newsTable .= ' <td>'.$news['date_created'].'</td>';
        $newsTable .= '<td id="updateNews'.$news["news_id"].'" class="newsUpdateButton updateButton text-center" type="submit" name="updateNews" action="index.php?page=admin-news"><i class="fa fa-pencil-square"></i></td>';
        $newsTable .= '<td id="deleteNews'.$news["news_id"].'" class="newsDeleteButton deleteButton text-center"  data-toggle="modal"><i class="fa fa-minus-circle"></i></td>';
        $newsTable .= '</tr>';
    }

    $view = include_once'views/admin-news-html.php';

    // This one does not work
    if(isset($_POST['updateNews'])){
         $updateId = $_POST['updateNews'];
         // Calling the function in the model
         $news->getNewsPost($updateId);
    }

这是模型:

    class Admin_NewsTable{
    private $dbh;

    // Connect to database
    public function __construct ( $pdo ) {
        $this->dbh = $pdo;
    }

    // This one works fine
    function getAllNews(){

    $sth = $this->dbh->prepare('SELECT news.news_id, news.title, news.content, news.date_created, users.username FROM news INNER JOIN users WHERE users.user_id = news.author_id');

    $sth->execute();

    // Set up an array to hold the individual post as an array
    $newsData = array();

    if($sth){
        while($r=$sth->fetch()){
            // Temp post array
            $tmpNews = array();

            $tmpNews['news_id'] = $r['news_id'];
            $tmpNews['title'] = $r['title'];
            $tmpNews['content'] = $r['content'];
            $tmpNews['date_created'] = $r['date_created'];
            $tmpNews['author_name'] = $r['username'];

            array_push($newsData, $tmpNews);

        }

        return $newsData;
    }

}


    // THis one is the problem
    public function getNewsPost($updateId){

    $sth = $this->dbh->prepare('SELECT news.news_id, news.title, news.content, news.date_created, users.username FROM news INNER JOIN users WHERE users.user_id = news.author_id AND news.news_id = ?');

    $sth->execute(array($updateId));

    if($sth){
        while($r=$sth->fetch()){
            // Temp post array
            $newsPost = array();

            $newsPost['news_id'] = $r['news_id'];
            $newsPost['title'] = $r['title'];
            $newsPost['content'] = $r['content'];
            $newsPost['date_created'] = $r['date_created'];
            $newsPost['author_name'] = $r['username'];

        }
    }

    return $newsPost;
}

它甚至在它进入模型之前就会中断,所以我做错了什么?

我使用相同的控制器和模型的另一个查询具有相同的结构,工作正常,所以我真的很困惑......

编辑:我设法通过从另一个控制器调用查询函数来解决问题。我有其他控制器,这种结构不会导致任何问题。任何人都可以向我解释为什么在这种特殊情况下这不起作用吗?

1 个答案:

答案 0 :(得分:2)

因为在foreach循环中你把新闻作为变量,它覆盖了新闻的对象价值并使其成为一个变量..

您可以尝试将变量名称更改为其他名称或更改对象名称。在相关行中使用

$obj = new Admin_NewsTable( $dbh );

并使用

调用类方法
$obj->getNewsPost($updateId);