Godfryd Godfryd - 7 months ago 11
PHP Question

How to get a single element in sql query?

I want to get user ip address based on users id. I don't get why it doesn't work.

In the controller:

function block_user($name,$user_id)
{
$ip = $this->m_db->get_ips($user_id);
$data = array(
'username' => $name ,
'ip' => $ip ,
);
$this->db->insert('blacklist', $data);

$this->db->where('user_id',$user_id);
$this->db->delete('comments');
$this->index();
}


In the model:

function get_ips($user_id)
{
$this->db->select('ip');
$this->db->from('users');
$this->db->where('user_id',$user_id);
$query = $this->db->get();
return $query;
}

Answer

CodeIgniter - return only one row?

What you should do is return a row

function get_ips($user_id)
{
    $this->db->select('ip');
    $this->db->from('users');
    $this->db->where('user_id',$user_id);
    $query = $this->db->get();
    $ret = $query->row();
    return $ret->ip;
}
Comments