1

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;
}
Godfryd
  • 479
  • 1
  • 7
  • 17

2 Answers2

1

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;
}
Community
  • 1
  • 1
0

First, you have to make all transaction database in your model, then you need to use

$this->db->get();

The correct way is: controller.php

function block_user($name,$user_id){
    $re = $this->m_db->get_ips($user_id, $name);

    echo $re;
}

model.php

function insert_data($ip, $name){
    $data = array(
                  'username' => $name ,
                  'ip'       => $ip ,
                  );

    $this->db->insert('blacklist', $data);

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

    return $this->db->affected_rows();
}

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

    $resp = $this->insert_data($query, $name);

    return $resp;
}
elddenmedio
  • 1,030
  • 1
  • 8
  • 15