简体   繁体   English

我们如何使用Codeigniter获取特定用户的记录

[英]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. 我想从我的订单号相等的数据库中获取所有记录,但是当尝试使用where条件时,它的显示空值请告诉我该怎么办。

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. 我确实不喜欢CodeIgniter的魔术功能来帮助数据库脚本编写,因为我无法真正控制正在发生的事情。 But if this might help, I am writing a raw MySQL query for you. 但是,如果这可能有所帮助,我正在为您编写一个原始的MySQL查询。 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). 然后可以从$query变量中获得模型中的结果(它将以对象形式)。 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. 要以数组形式获取结果,可以运行$data = $query->row_array()将结果作为数组推送到变量$data中。

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. 如果结果可能返回多于1行,那么您应该取出LIMIT 1表达式并使用foreach循环来获取结果。

Hope it answers your question 希望它能回答您的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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