codeigniter模型中的php未定义方法

ac1kyiln  于 2021-06-23  发布在  Mysql
关注(0)|答案(3)|浏览(265)

我已经做了一个管理页面和用户页面,我想显示谁在数据库中注册的管理员登录时,用户名单。为此,我创建了一个模型,如下所示,

public function regi_users(){
$q = $this->db->query("SELECT username FROM public");

return $q;
}

我通过我创建的一个视图访问它,当管理员登录时,他会被重定向到这个视图,如下所示,
帐户.php

<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
 </head>
 <body>
<?php

  $this->load->model('loginmodel');
    $qresult = $this->loginmodel->regi_user();

    foreach ($qresult as $row) {
      echo $row->username;
    }
 ?>

 </body>

但是当我的管理员登录时,他显示了以下错误,
致命错误:在e:\wamp64\www\ci\application\controllers\account.php的第11行调用未定义的方法loginmodel::regi\u user()
我做错什么了?我很抱歉,如果这是一个愚蠢的问题,但我是一种新的php
谢谢你的建议

nwnhqdif

nwnhqdif1#

您的查询应该是这样的

public function regi_users(){
   return $this->db->select('username')->from('table_name')->get()->result_array();
}
628mspwn

628mspwn2#

控制器

class User extends CI_Controller 
{
   public function __construct() 
   {
      parent::__construct();        
      $this->load->model('YourModelName');
   }
    public function fetchUser()
    {
        $data['user']=$this->YourModelName->fetchUsers();
        $this->load->view('yourViewClass',$data);
    }
}

模型

class YourModelName extends CI_Model
{
    function __construct() 
    {
        $this->load->database();              
    }

    public function fetchUsers() 
    {
        $query = $this->db->select('*')->from('table_name');
        $query=$this->db->get();
        if($query->num_rows()>0)
        {
            $result=$query->result();
            return $result;
        }else{ 
            return 0;
        }
    }
}

看法

<table>
  <thead>  
    <tr>
      <th>Firstname</th>         
    </tr> 
   </thead> 
   <tbody> 
     <?php  foreach ($user as $row) { ?>   
       <tr>
         <td><?php echo $row->username; ?></td>   
       </tr>     
     <?php } ?> 
  </tbody> 
</table>
laik7k3q

laik7k3q3#

控制器

class User extends CI_Controller {
   public function __construct() {
      parent::__construct();        
      $this->load->model('loginmodel');
   }

  public function FunctionName($value='')
  { 
      $this->data["users"] = $this->loginmodel->regi_user();        
      $this->load->view('account',$this->data);
  }
}

型号

class Loginmodel extends CI_Model{
  function __construct() {
    parent::__construct();              
  }

  public function regi_user() {
    $query = $this->db->get('table_name')->result();
    return $query;
  }
}

查看

<table class="table">
<thead>
  <tr>
    <th>Firstname</th>       
  </tr>
</thead>
<tbody>
<?php  foreach ($users as $row) { ?>
  <tr>
    <td><?php echo $row->username; ?></td>
  </tr>  
  <?php } ?>
</tbody>
</table>

相关问题