无法从MySQL数据库表中检索公司名称

时间:2017-05-03 05:52:59

标签: php mysql codeigniter codeigniter-2 codeigniter-3

我正在尝试检索公司名称并将其存储在CodeIgniter中的会话中,但我无法从表pr_users_details中检索公司字段。

row1->company应该给我公司名称,但事实并非如此。

<?php
Array
(
[session_id] => ed64aacc3975c359615569ff9c0bedb1
[ip_address] => ::1
[user_agent] => Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36
[last_activity] => 1493791463
[user_data] => 
[is_admin_logged_in] => 1
[admin_user_name] => admin@google.com
[admin_userpass] => 65e1b655a6d6f4cbed20554d3b52521a743afdc0
[admin_id] => 23
[admin_lastlogin] => 03-05-2017 07:30:37
[admin_lastloginip] => ::1
[lastrefresh] => 1493791470
[company] => 
)
?>

这里公司没有价值

function check_admin_login()
{

    $this->db->where('username', trim($this->input->post('username')));
    $this->db->where('userpass ', sha1(trim($this->input->post('userpass'))));
    $this->db->where('status', '1');
    $this->db->where('deleted', '0');
    $this->db->select('*');
    $query = $this->db->get($this->myTables['users']);
    if ($query->num_rows() > 0) {
        $row = $query->row();
        $this->db->where('userid', $row->id);
        $this->db->select('firstname,lastname,profileimage,company');
        //$query1 = $this->db->query("Select * from pr_users_details where userid = '".$row->id."'");
        $query1 = $this->db->get($this->myTables['users_details']);
        $row1 = $query1->row();
        $newdata = array(
            'is_admin_logged_in' => true,
            'admin_user_name' => $row->username,
            'admin_userpass' => $row->userpass,
            'admin_id' => $row->id,
            'admin_lastlogin' => date("d-m-Y H:i:s", $row->lastlogin),
            'admin_lastloginip' => $row->lastloginip,
            'lastrefresh' => time(),
            'company' => $row1->company
        );
        $company = $row1->company;
        $this->session->set_userdata($newdata);
        $companyName = $this->session->userdata('company');
        echo "<pre>";
        print_r($this->session->all_userdata());
        echo "</pre>";
        die;

        $this->update_admin_login_time($this->session->userdata('admin_id'));
        $this->admin_init_elements->set_global_user($row->username, $row->userpass);
        if ($this->input->post('remember')) {
            $cookie = array('name' => 'username', 'value' => $row->username, 'expire' => time() + 7600, 'secure' => false);
            $this->input->set_cookie($cookie);
        }
        $name = $row1->firstname . ' ' . $row1->lastname;
        $cookie1 = array('name' => 'name', 'value' => $name, 'expire' => time() + 7600, 'secure' => false);
        $this->input->set_cookie($cookie1);
        $cookie2 = array('name' => 'image', 'value' => $row1->profileimage, 'expire' => time() + 7600, 'secure' => false);
        $this->input->set_cookie($cookie2);


        return 'Login Successful';
    } else {
        return 'Incorrect Username or Password.';
    }

}

打印会话时,我得到了这个:

enter image description here

2 个答案:

答案 0 :(得分:0)

请您在数据库表中检查用户是否有公司名称,因为我看到表中有很多公司名称字段为空。

如果有数据,请尝试以下代码:

$ ROW1 [0] - &GT;公司;

答案 1 :(得分:0)

在这里,您需要应用您的调试技能。您可以按如下方式调试代码:

首先,您可以通过应用静态公司名称进行检查,如下所示:

$newdata = array(
            'is_admin_logged_in' => true,
            'admin_user_name' => $row->username,
            'admin_userpass' => $row->userpass,
            'admin_id' => $row->id,
            'admin_lastlogin' => date("d-m-Y H:i:s", $row->lastlogin),
            'admin_lastloginip' => $row->lastloginip,
            'lastrefresh' => time(),
            'company' => 'static_abc_company'
        );

现在,如果您能够在会话中检索它,那么您的数据库查询/数据就会出现问题,因此您可以打印由DB查询获取的数据,如下所示,并检查:

 $this->db->where('userid', $row->id);
        $this->db->select('firstname,lastname,profileimage,company');
        //$query1 = $this->db->query("Select * from pr_users_details where userid = '".$row->id."'");
        $query1 = $this->db->get($this->myTables['users_details']);
        $row1 = $query1->row(); 
        print_r($row1->result_array);

如果您没有从数据库中查看公司数据,请检查数据库中的数据。