[英]Codeigniter active record class join two mysql tables
Just want to get user_id and review_text values from table reviews , select name and city from table users where id=user_id and then return review_text, username and city to controller. 只想从表评论中获取user_id和review_text值,从表用户中选择id和user_id的 名称和城市 ,然后将review_text,用户名和城市返回给控制器。 Please help me. 请帮我。
public function did_get_reviews($item_id){
$this->db->select('user_id','review_text');
$this->db->from('reviews');
$this->db->where('post_id', $item_id);
$user_id = 'user_id' //??
$this->db->select('name','city');
$this->db->from('users');
$this->db->where('id', $user_id);
$query = $this->db->get('review_text','username','city'); //??
if ($query && $query->num_rows() >= 1){
return $query->result();
}
else {
return false;
}
}
Update 1: I just tried to join tables but it returns the values for review_text only but I also want to get values for name and city . 更新1:我只是试图联接表,但它仅返回review_text的值,但我也想获取name和city的值。 Please check the code below: 请检查以下代码:
public function did_get_reviews($item_id){
$this->db->select('reviews.review_text','users.name','users.city');
$this->db->from('reviews','users');
$this->db->where('reviews.post_id', $item_id);
$this->db->join('users','reviews.user_id = users.user_id');
$query = $this->db->get();
if ($query && $query->num_rows() >= 1){
return $query->result();
}
else {
return false;
}
}
i think you need to use join query in SQL. 我认为你需要使用SQL 联接查询。 if you use this code you can access to what you want 如果您使用此代码,则可以访问所需的内容
$result = $this->db->select('review, username, city')
->from('reviews')
->join('city', 'city.user_id = review.user_id')
->get();
print_r($result);
for more information about how you can write join query(left, inner or right) in codeigniter you can see this link 有关如何在codeigniter中编写联接查询(向左,向内或向右)的更多信息,请参见此链接
i hope that this code solve your problem 我希望这段代码可以解决您的问题
UPDATE UPDATE
in your new question. 在您的新问题中。 your code have a little buggy. 您的代码有一些错误。 you need to write your select in this way 您需要以这种方式编写选择
public function did_get_reviews($item_id){
$this->db->select('reviews.review_text,users.name,users.city')
->from('reviews','users')
->where('reviews.post_id', $item_id)
->join('users','reviews.user_id = users.user_id');
$query = $this->db->get();
if ($query && $query->num_rows() >= 1){
return $query->result();
}
else {
return false;
}
}
in codeigniter select Active record; 在codeigniter中选择活动记录; any column name separate with each other by ,
only (not by '
and ,
) 任何列名称彼此通过分离,
仅(不是由'
和,
)
in codeigniter documentation wrote that 在codeigniter 文档中写道
Permits you to write the SELECT portion of your query: 允许您编写查询的SELECT部分:
$this->db->select('title, content, date'); $ this-> db-> select('title,content,date');
$query = $this->db->get('mytable'); $ query = $ this-> db-> get('mytable');
// Produces: SELECT title, content, date FROM mytable //产生:SELECT标题,内容,日期from mytable
You can get the value for user_id
using: 您可以使用以下方法获取user_id
的值:
$this->db->select('user_id','review_text');
$this->db->from('reviews');
$this->db->where('post_id', $item_id);
$result = $this->db->get();
print_r($result);
use the same $this->db->get();
使用相同的$this->db->get();
to get the values of the second query as well. 以获取第二个查询的值。 Once you get the values inside a variable you can iterate it. 一旦在变量中获取值,就可以对其进行迭代。 Something like: 就像是:
foreach ($result->result() as $row)
{
echo $row->name;
echo $row->city;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.