2017-03-02 81 views
-1

我試圖從我的MySQL數據庫中拉出多行數據並在我的網頁上顯示其內容時收到錯誤。因爲我是很新的代碼點火器,我不知道如何解決這個錯誤:嚴重性:通知消息:嘗試獲取非對象的屬性

嚴重性:注意 消息:試圖讓非對象

這裏的屬性是與錯誤相關的代碼。

型號:

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 

class news_model extends CI_Model 
{ 
    function __construct() 
    { 
     parent::__construct(); 
    } 

function get_news() 
{ 
    $query = $this->db->query("SELECT * FROM news WHERE active = 1"); 
    if ($query->num_rows() > 0) 
    { 
     foreach($query->result() AS $row) 
     { 
      $array[] = get_object_vars($row); 
     } 
     return $array; 
    } 
} 

}?> 

控制器:

<?php 
class profile extends CI_Controller 
{ 
    public function __construct() 
    { 
     parent::__construct(); 
     $this->load->helper(array('url','html')); 
     $this->load->library('session'); 
     $this->load->database(); 
     $this->load->model('user_model'); 
     $this->load->model('news_model'); 
    } 

    function index() 
    { 
     $details = $this->user_model->get_user_by_id($this->session->userdata('uid')); 
     $data['uname'] = $details[0]->fname . " " . $details[0]->lname; 
     $data['uemail'] = $details[0]->email; 
     $data['ustorenumber'] = $details[0]-> storenumber; 
     $data['urank'] = $details[0]-> rank; 
     $data['news'] = $this->news_model->get_news(); 
     $this->load->view('profile_view', $data);//Error reported on this line. 
    } 
} 

查看:

   <?php 
       foreach($news AS $row) { 
        echo '<p>' . $row->content . '</p>';//Error reported on this line 
       } 
      ?> 

完全錯誤:

A PHP Error was encountered 

Severity: Notice 

Message: Trying to get property of non-object 

Filename: views/profile_view.php 

Line Number: 82 

Backtrace: 

File: C:\xampp\htdocs\cig\application\views\profile_view.php 
Line: 82 
Function: _error_handler 

File: C:\xampp\htdocs\cig\application\controllers\profile.php 
Line: 22 
Function: view 

File: C:\xampp\htdocs\cig\index.php 
Line: 315 
Function: require_once 

回答

0

在對象format.Like這個模型迴歸結果..

function get_news() 
{ 
    $query = $this->db->query("SELECT * FROM news WHERE active = 1"); 
    if ($query->num_rows() > 0) 
    { 
     return $query->result();//returns result in object format 
    } 
} 

OR

鑑於:使用$row['content'] insetad的$row->content;

<?php 
      foreach($news AS $row) { 
       echo '<p>' . $row['content']. '</p>';//Error reported on this line 
      } 
     ?> 
相關問題