Codeigniter:从数组中追加的值

时间:2013-12-09 19:17:20

标签: php mysql sql arrays codeigniter

public function func1()
{
    $this->func();
    $result1 = $this->result;

    foreach ($result1 as $row) {
        $this->load->model('test_model');
        $this->db->select('name');
        $this->db->from('place');

        $string = '';

        $string = $row[state_name];

        $this->db->where('state'=,"$string");
        $query = $this->db->get();

        return $query->result_array();
    }

}

其中$this->func();获取一个包含名称字符串数组的数组,我在where func1(select)子句中使用每个值作为条件。我使用A foreach循环从func的结果集中获取每个字符串,然后我尝试选择与func1中的where子句匹配的名称但我我低于错误

A Database Error Occurred
    Error Number: 1064
    SELECT `name` FROM (`place`) WHERE `state=` 'place1,place2, place3,place4';

来自foreach的所有值都会附加到同一个查询中,在我的情况下,每个迭代应该只获取一个值并执行SELECT查询并返回结果。

1 个答案:

答案 0 :(得分:1)

你可以试试这个:

public function func1()
{
    $this->func();
    $result1 = $this->result;

    $data = null; $i=0;
    foreach ($result1 as $row) {
        $this->load->model('test_model');
        $this->db->select('name');
        $this->db->from('place');

        $string = '';

        $string = $row['state_name'];

        $this->db->where('state'=,"$string");
        $query = $this->db->get();

        $query->result_array();
        $data[$i] = $query->result_array;
    }
  return $data;

}