如何在CI中基于2个查询获得结果

时间:2015-09-26 17:00:34

标签: php mysql codeigniter

抱歉这个令人困惑的标题,因为我真的不知道输入什么。我是CI的新手,现在我正在尝试将我的代码转换为CI并停留在此处。

这是我的原始代码:

$ tail -n 2 outputfile 
AI9xRoPQ 00:00:00,007
LEAeMTva 00:00:00,005

所以基本上它请求域id为X的域 然后它根据域表中的所有者ID请求所有者的数据。

我不确定在控制器或模型中放入什么 但这是我现在的模特:

$query_domain = $konek->prepare("SELECT * FROM `domain` WHERE `id` = :id");
$query_domain->bindParam(":id", $id);
$query_domain->execute();
$data_domain = $query_domain->fetch();

$query_owner = $konek->query("SELECT * FROM `people` WHERE `id` = $data_domain->ownerid");
$data_owner = $query_owner->fetch();

2 个答案:

答案 0 :(得分:0)

在您的(客户)模型中:

public function get_owner_with_domain($domain)
{
  $query = $this->db->select('c.*')->
  from('client c')->
  join('domain d', 'd.ownerid = c.id')->
  where('d.name',$domain)->get();

  if ($query) {
    return $query->row_array();
    // Or, ideally return a client if you have a Client model
    // return $query->row(0,'Client'); 
  } else {
    // log error?
    return false;
  }
}

在您的控制器中:

public function client($domain) // or whatever your function might be called
{
  $this->load->model('Client'); 
  $client = $this->Client->get_owner_with_domain($domain)
  // Do something with the client
  // var_dump($client);
  $view_data['client'] = $client;
  $this->load->view('client_info',$view_data);
}

进一步阅读:

CodeIgniter ActiveRecord reference

CodeIgniter Results reference

答案 1 :(得分:0)

您只需使用一个查询

即可
SELECT people.* FROM people, domain WHERE people.id = domain.ownerId AND domain.id = :id