php - Codeigniter:从数组附加值

标签 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查询并返回结果。

最佳答案

你能试试这个吗:

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;

}

关于php - Codeigniter:从数组附加值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20478814/

相关文章:

php - 使用 php 在字符串中降级标题标签(递归地将 h1 转换为 h2,将 h2 转换为 h3)

php - Javascript window.location.href 重定向不起作用

php - 使用 PHP 爆炸来自 mysql 的数据

mysql - mysql_insert_id 线程安全吗?

php - 使用左连接连接多个表

mysql - 从 csv 导入时 mysql 出错

sql - 显示包含特定列的所有表

php - Laravel foreach 在 View 中

sql - 存储过程计算和性能改进

mysql - 如何使用 LIMIT 从 MYSQL 中选择包含一个具有值的元素的数据,例如。 1