dskm94301
2017-09-20 11:44
浏览 59
已采纳

如何使用user_id从数据库中获取用户详细信息并显示它们

I'm making a website on the CodeIgniter framework so users can add products to the website. Now my question is: How do I use the user_id in the products table to get the user's details from the database and display them next to the product? So for example when I click on a product I want to have a link to the profile of the user who uploaded the product but I'm not sure how I can do that.

Database tables information:

table users:

user_id (primary_key)
email
voornaam
achternaam
beschrijving


table products:

product_id (primary_key)
product_naam
product_beschrijving
user_id
category_id

My function in model file:

public function get_product_details($product_id) {
    $this->db->select('products.*, users.*');
    $this->db->from('products');
    $this->db->join('users', 'users.user_id = products.user_id', 'left');
    $this->db->where('product_id', $product_id);
    $query = $this->db->get();
    $result = $query->row_array();
    if (!empty($result)) {
        return $result;
    } else {
        return array();
    }
}
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题