非法字符串偏移' name'在CodeIgniter中

时间:2014-09-02 18:27:27

标签: php codeigniter model

我有这个错误:

A PHP Error was encountered
Severity: Warning
Message: Illegal string offset 'name'
Filename: user/account.php
Line Number: 72

在控制器“account.php”中我有这个:

[...]
$account['account']  = $this->user_model->account($id);

$this->template->write_view('top_menu', 'top_menu');
$this->template->write_view('user', 'template/user/panel', $data, TRUE);
$this->template->write_view('left', 'template/public/menu');
$this->template->write_view('right', 'template/user/account',$account, TRUE);
$this->template->render();

在“user_model.php”中我有这个:

public function account($id)
{
    $query = $this->db->get_where('users', array('id' => $id));

    if ($query->num_rows() == 1)
    {
        return $query->row();
    }
    else
    {
        return FALSE;
    }
}

在视图中我有这个:

    <?php foreach ($account as $row)
    {
        (this is the line 72) echo $row['name'];
    }
    ?>

什么是错误?谢谢你的帮助!!!

1 个答案:

答案 0 :(得分:0)

尝试使用此名称来调用名称

<?php 
  echo $account->name;
?>

修改

像我上面的回答一样保持视野,并尝试像这样改变你的模型

public function account($id)
{
    $query = $this->db->where(array('id' => $id))->limit(1)->get('users');

    if($query->num_rows())
        return $query->row();
    else
        return FALSE;
}