1

我是codeigniter编程的新手。当我尝试连接数据库并从表中检索数据时,我发现如下错误:

A PHP Error was encountered
Severity: Notice
Message: Undefined property: CI_DB_mysql_result::$result
Filename: models/haii.php
Line Number: 15.

A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: views/getquery.php
Line Number: 2

我正在使用此代码使用 codeigniter 与数据库连接:

控制器:

<?php
class product extends CI_Controller
{
    function __construct()
    {
    }
    function getall()
    {
        $this->load->model('haii');
        $data['query']=$this->haii->you();
        $this->load->view('getquery',$data);
    }
}
?>      

模型:

<?php
class haii extends CI_Model
{
    function __construct()
    {
        parent::__construct();
        $this->load->database();
    }
    function you()
    {
        $query=$this->db->get('member');
        return $query->result;
    }
}
?>

看法:

<?php
foreach($query as $row)
{
    print $row->username;
    print $row->password;
    print $row->fname;
    print $row->lname;
    print $row->contact;
    print "<br>";
}
?>
4

3 回答 3

8

在你的函数 中使用return $query->result();而不是。是一种方法,而不是类成员。return $query->result;you()result

于 2013-06-21T12:27:58.800 回答
1

更改您的模型功能

function you()
{
    $query=$this->db->get('member');
    return $query->result;
}

return $query->result();

它将解决您的问题。

于 2014-11-22T12:09:19.547 回答
0

也许可以修复它

<?php
class haii extends CI_Model
{
    function __construct()
    {
        parent::__construct();
        $this->load->database();
    }
    function you()
    {
        $query=$this->db->get('member');
        return $query;
    }
}
?>

在视图中

<?php
foreach($query->result() as $row)
{
    print $row->username;
    print $row->password;
    print $row->fname;
    print $row->lname;
    print $row->contact;
    print "<br>";
}
?>
于 2013-06-22T05:48:53.393 回答