0

这里有两个样本,

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
4

3 回答 3

1

在您看来,情况 1 应该是

<?php echo $article->row->title; ?>

使代码与情况2相同。

如我所见, $article 是行的集合,因此您必须从文章中访问一行,然后获取该行的标题。

于 2012-09-22T19:58:48.553 回答
0

在获取标题之前尝试序列化()“行”。因为您的查询,我猜,返回一个 xml 格式(如果我错了,请纠正我)。

于 2014-02-07T09:02:58.420 回答
0

对于那些有这个问题的人,请在 \system\libraries\Parser.php 中替换:

$template = $this->_parse_single($key, (string)$val, $template);

至:

if (!is_object($val)) $template = $this->_parse_single($key, (string)$val, $template);
于 2016-09-05T12:51:35.907 回答