Php 如何在sql查询中获取单个元素?

Php 如何在sql查询中获取单个元素?,php,codeigniter,Php,Codeigniter,我想根据用户id获取用户ip地址。我不明白为什么它不起作用 在控制器中: 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); $thi

我想根据用户id获取用户ip地址。我不明白为什么它不起作用

在控制器中:

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

您应该做的是返回一行

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

首先,必须在模型中创建所有事务数据库,然后需要使用

$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;
}