2016-04-17 5 views
1

私はユーザーのIDに基づいてユーザーのIPアドレスを取得したい。私はなぜそれが動作しない取得しません。コントローラでSQLクエリで単一の要素を取得するには?

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(); 
} 

モデルで:

あなたは何をすべき
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; 
} 

答えて

1

CodeIgniter - return only one 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; 
} 
0

最初の行を返すことですモデル内のすべてのトランザクションデータベースを作るために、あなたは

$this->db->get(); 

を使用する必要が正しい方法は次のとおりです。 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; 
} 
関連する問題