【问题标题】:Object of class stdClass could not be converted to string in codeigniter类 stdClass 的对象无法在 codeigniter 中转换为字符串
【发布时间】:2016-09-05 20:51:35
【问题描述】:

这里有两个示例,

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

【问题讨论】:

  • 是的,请接受一些答案。然后,给我们这个输出:print_r($result_article-&gt;row());
  • 我已经编辑了我的 ans 并包含了 print_r 输出。

标签: php codeigniter


【解决方案1】:

在您看来,情况 1 应该是

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

使代码与情况 2 相同。

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      对于有此问题的用户,请在 \system\libraries\Parser.php 中替换:

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

      收件人:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-02-21
        • 1970-01-01
        • 2013-12-13
        • 2011-04-06
        • 2018-10-19
        相关资源
        最近更新 更多