【问题标题】:Using where_in() with a multi-dimensional array将 where_in() 与多维数组一起使用
【发布时间】:2013-12-30 21:41:47
【问题描述】:

我想知道如何将 where_in() 函数中内置的 CodeIgnitors 与多维数组一起使用。到目前为止,我是这样做的,但我得到一个数组字符串转换错误。

示例数组:(数组是动态的,会根据用户输入的内容而变化)

array([0]=>'hello',[1]=>'goodbye');

我的模特:

 $this->db->where_in('Column',$array);
        $query = $this->db->get('Table');
        $result = $query->result();
        foreach ($result as $row) {
            $vID = $row->ID;
            $this->db->insert('Table2', array('ID' => $id, 'val' => $vID));
        }

我收到以下错误:

Message: Array to string conversion

Filename: database/DB_active_rec.php

Line Number: 555

还有这个:

Error Number: 1054

Unknown column 'Array' in 'where clause'

SELECT * FROM (`Tags`) WHERE `Tags` IN (Array) 

【问题讨论】:

  • 您的$array 是什么样的?你确定它是“多维”的吗? where_in 不适用于多维数组。此外,您不能在 insert 中执行 val' => $array。也许你想要$this->db->insert_batch
  • 对不起,这是一个错误,使用多维数组的解决方法是什么?
  • 你的数组真的是什么样子的?您可能需要 foreach 覆盖它并为里面的每个数组运行它。

标签: php arrays codeigniter activerecord


【解决方案1】:

当人们使用 CI 而只是不阅读指南时,我总是感到沮丧,它明确指出您只能使用以下内容:

  1. 简单的键/值(名称=某物)
  2. 自定义键/值(名称!=某事)
  3. 关联数组(name => something)
  4. 自定义字符串("name = 'something'")

您不能使用 “多维” 数组,而只是将其折腾 让它为您工作。请在此处阅读指南:http://ellislab.com/codeigniter/user-guide/database/active_record.html

【讨论】:

    猜你喜欢
    • 2016-10-13
    • 2014-03-06
    • 1970-01-01
    • 2012-12-13
    • 2013-01-09
    • 2021-12-14
    • 1970-01-01
    • 1970-01-01
    • 2021-03-08
    相关资源
    最近更新 更多