将数据库结果转换为Codeigniter中的数组 [英] converting database result into array in codeigniter

查看:43
本文介绍了将数据库结果转换为Codeigniter中的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Codeigniter的新手,我正在尝试制作一个具有admin,主持人和用户角色的登录身份验证系统.

I am new to codeigniter and I am trying to make a login authentication system with the role of admin, moderator, and user.

我的问题是我试图在数据库中的列角色"上设置一个条件.但是我不知道如何使用和比较列角色的价值.

My problem is that I am trying to set a condition on 'role' which is a column in database. But I don't know how to use and compare the value of column role.

我编写了以下代码,但出现错误:

I wrote the following code but I am getting error:

[严重性:错误消息:调用数组中的成员函数result_array()]

[Severity: Error Message: Call to a member function result_array() on array]

我的代码是:

<?php
// step no 4 create a new controller where form post after submission.
class verify extends CI_Controller{
     function __construct(){
        parent::__construct();
         $this->load->helper('url');
         $this->load->model('user');
     }
    function login(){
        //step no 5 create a new method in verify controller 
     
       $username = $this->input->post('username');
       $userpass = $this->input->post('password');
       //step 9 take value back in answer variable from model and print message 
      $answer = $this->user->usergetdata($username,$userpass,'admin');   
     $data = $answer->result_array();


        if($answer){
            if($data[0]['role']=='admin'){
              redirect('admin_page');  
            }
            
            
        }else
        {
            echo "username or Password is wrong";
        }
    }
    
}

?>

<!-- begin snippet: js hide: false console: true babel: false -->

推荐答案

模型中

$res = $this->db->get(); 
return $res->result_array();

然后申请

foreach($answer as $a)
{
  var_dump( $a );
} 

使用 $ a 获取值

这篇关于将数据库结果转换为Codeigniter中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆