这里有两个样本,
1.
在控制器中
$result_article = $this->cms->get_content($event_id);
if($result_article->num_rows() == 1){
$data['row'] = $result_article->row();
}
在我看来
<?php echo $row->title; ?>
print_r($row)/print_r($result_article->row()); 输出
stdClass Object ( [id] => 43 [title] => Grant visit by Prime Minister [content] => this is the content [create_date] => 2012-09-21 [last_update] => 2012-09-22 19:27:12 )
出现错误
A PHP Error was encountered
Severity: 4096
Message: Object of class stdClass could not be converted to string
Filename: libraries/Parser.php
Line Number: 101
2.
在控制器中
$result_article = $this->cms->get_content($event_id);
if($result_article->num_rows() == 1){
$row = $result_article->row();
$data = array(
'title' => $row->title
);
}
在我看来
<?php echo $title; ?>
print_r($title) 输出
Grant visit by Prime Minister
没有错误。
第一个控制器和第二个代码有什么不同,它应该是相同的输出吧?我很困惑!
为了更清楚,我简化了代码
这是模型
function get_content($event_id){
$this->db->select('id,title,content,create_date,last_update');
$query = $this->db->get_where('events',array('id' => $event_id));
return $query;
}
控制器
$data['query'] = $this->cms->get_content($this->uri->segment(3));
if($data['query']->num_rows() == 1){
$row = $data['query']->row();
<!--with this code, no error appear-->
$data = array(
'title' => $row->title,
'content' => $row->content,
'create_date' => $row->create_date,
'last_update' => $row->last_update,
'event_id' => $row->id
);
<!--with this code, no error appear-->
如果只有这些代码
$data['query'] = $this->cms->get_content($this->uri->segment(3));
if($data['query']->num_rows() == 1){
$row = $data['query']->row();
发生错误,
A PHP Error was encountered
Severity: 4096
Message: Object of class CI_DB_mysql_result could not be converted to string
Filename: libraries/Parser.php
Line Number: 101