简体   繁体   中英

How we get record for particular user using codeigniter

I want to fetch all records from database where my order number is equal, but when am trying to use where condition then its show null value please tell me what can I do.

model code:

public function get_products()  
{

    $this->load->database();
    $this->db->select('product');
    $this->db->select('sku');
    $this->db->select('price');
    $this->db->select('qty');
    $this->db->select('tax');
    $this->db->select('total');
    $this->db->from('data');
    $this->db->where('order',$this->input->post('order'));
    $query = $this->db->get();
    if($result = $query->result_array())
    {
        return $result;
    }
    else
    {
        return false;
    }
}

Try this code

MODEL:

function single_user_data($user_id)
{
    $this->db->select('');
    $this->db->from('user');            
    $this->db->where('user_id',$user_id);
    $query=$this->db->get()->result();
    return $query;  
}

CONTROLLER

public function user_data_get()
    {
        $user_id=$this->input->post('id');  
        $data['user_data']=$this->User_Model->single_user_data($user_id);   
        $this->load->view('edit_user.php',$data);   
    }

I really am not a fan of CodeIgniter's magic functions to help in database scripting because I can't really control what is happening. But if this might help, I am writing a raw MySQL query for you. This might prove to be a little faster (for reasons I will explain later)

CONTROLLER

class Test extends CI_Controller
{
    //.... some functions come here

    public function user_data_get()
    {
        $this->load->model('Get_user_model');
        $user_data = $this->Get_user_model->get_product($this->input->post('order'));
    }
}

MODEL

class Get_user_model extends CI_Model
{
    //..some code comes here

    public function get_product($order)
    {
        $order = $this->db->escape($order);    // Prevention against SQL injection

        $query = $this->db->query("
            SELECT `product`, `sku`, `price`, `qty`, `tax`, `total` FROM `data`
            WHERE `order` = " . $order . " LIMIT 1");
    }
}

The results in the model can then be obtained from the $query variable (it will be in an object form). To get the result in an array form, you can run $data = $query->row_array() to push the results into a variable $data as an array.

If the results may return more than 1 row, then you should take out the LIMIT 1 expression and use a foreach loop to get the results.

Hope it answers your question

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM