简体   繁体   English

从Codeigniter中的关系数据库中获取数据

[英]Fetching data from relational database in codeigniter

I have got 2 tables; 我有2张桌子。 table1 and table2. table1和table2。 Both of them are related to eachother with a common groupid I am able to query the second table successfully using the following code: 他们两个都使用共同的groupid相互关联,我可以使用以下代码成功查询第二张表:

$query = $this->db->query("SELECT * FROM `table2` WHERE `memberid`='$id'"); 
$data['relation'] = $query->result_array(); 

Now using groupid result I want to query the first table ie table1 现在使用groupid结果我要查询第一个表,即table1

I have tried the following methods, without any success: 我尝试了以下方法,但没有成功:

for($ii = 0 ; $ii < count($data['relation']) ; $ii++){
        $id = $data['relation'][$ii]['groupid'];
        $query1 = $this->db->query("SELECT * FROM `group` WHERE `id`='$id'");
}             

$data1['group'] = $query1->result_array();

$fine = array_merge($data, $data1);
print_r(count($fine)); // the count result is 1 ideally should be 2

The above code only returns the last row of the table1 however I am looking for all the results. 上面的代码仅返回table1的最后一行,但是我正在寻找所有结果。

When I run the above code inside the "for" loop, it shows me a count of 33: 当我在“ for”循环内运行以上代码时,显示为33:

    for($ii = 0 ; $ii < count($data['relation']) ; $ii++){
        $id = $data['relation'][$ii]['groupid'];
        $query1 = $this->db->query("SELECT * FROM `group` WHERE `id`='$id'");

$data1['group'] = $query1->result_array();

$fine = array_merge($data, $data1);
print_r(count($fine)); // the count result is 33 ideally should be 2
}             

I know how to achieve this in core php however not too sure how to do it in CI. 我知道如何在核心php中实现此目标,但不太确定如何在CI中做到这一点。 I am new to CI, any help will be greatly appreciated. 我是CI的新手,我们将不胜感激。

Thanks, Utpal 谢谢,乌塔尔

$ok = array();
     for($ii = 0 ; $ii < count($data['relation']) ; $ii++){
            $id = $data['relation'][$ii]['groupid'];
            print_r ($id);
            echo "<br>";
            $query1 = $this->db->query("SELECT * FROM `group` WHERE `id`='$id'");
            $data1['group'][$ii]= $query1->result_array();

    }
        //print_r($data1['group']);
        $fine = array_merge($data, $data1);
        print_r($fine);

You need to create a array ($data1) outside this for loop and define $query->result_array(); 您需要在此for循环之外创建一个数组($ data1),并定义$ query-> result_array();。 inside forloop as shown above 在forloop内部,如上所示

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

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