使用来自单独数据库的数组筛选数据库查询结果


Filtering Database query results with array from separate database

我有一堆网站,都是从一个单一的数据库提取数据。所有这些几乎是相同的,并建立在CodeIgniter。

每个站点都有一个单独的数据库,其中包含特定于该站点的数据。

通用数据库:表'courses' =列:id, course_name, price, external_link, tracking_link

站点特定数据库:表'courses' =列:id, active, description

有一个课程和课程数据池,每个站点从通用数据库中提取,但是每个站点可以确定哪些课程是"活动的"。我可以从站点特定数据库中获得"active"= 1的行的所有id,并将它们传递给一个数组。

然后我想使用来自该查询的id作为通用数据库上的过滤器,将课程数据拉入站点的各个部分。

这是我的模型:
public function getActiveCourses() {
    $local_db = $this->load->database('local', TRUE);
    $universal_db = $this->load->database('universal', TRUE);       

    $activeCourses =    $local_db
                ->where('active', 1)
                ->select('id')
                ->get('courses');
    $activeList = $activeCourses->result_array();

    $allCourses =   $universal_db
            ->where('id', $activeList)
            ->get('courses');
    return $allCourses->result_array();
}

但是我得到以下错误

 Error Number: 1054
 Unknown column 'Array' in 'where clause'
 SELECT * FROM (`courses`) WHERE `id` = Array
 Filename: /models/courses.php
 Line Number: 39

第39行是:

$allCourses =       $universal_db
            ->where('id', $activeList)
            ->get('courses');  <== Line 39
return $allCourses->result_array();

我一直在搜索和玩这个好几个小时。非常感谢您的帮助。

是CI和php的新版本,因此可以在必要时提供任何进一步的细节。我一时拿不准,最需要的是什么。

特雷

好吧,$activeList是一个数组,但你需要提供一个单一的值代替:

$activeList = $activeCourses->result_array(); // see, "result_array"
// here you're returning the row in array format (array ['id'] =>...)
$allCourses =   $universal_db
            ->where('id', $activeList)  // here are you using the whole array!
            ->get('courses');

应该是这样的:

$allCourses =   $universal_db
            ->where('id', $activeList['id']) // passing the specific array index
            ->get('courses');