使用Kohana PHP在2个数据库中选择

时间:2014-09-01 21:22:25

标签: php database kohana kohana-3.3 mysql-error-1146

如何在Kohana的不同数据库中加入表?

$tb_new = 'db_zaboo_feed.feed_' . $feed;
            $ids = DB::query(Database::SELECT, 
                             "SELECT d.fuid_id, d.user_id FROM db_zaboo.displays d
                             LEFT JOIN $tb_new f ON(d.fuid_id = f.uid)
                             WHERE d.user_id = (SELECT user_id FROM $tb_new GROUP BY user_id) AND f.uid IS NULL")->execute();

ERROR:

Database_Exception [ 1146 ]: Table 'db_zaboo.displays' doesn't exist [ SELECT d.fuid_id, d.user_id FROM db_zaboo.displays d LEFT JOIN db_zaboo_feed.feed_3 f ON(d.fuid_id = f.uid) WHERE d.user_id = (SELECT user_id FROM db_zaboo_feed.feed_3 GROUP BY user_id) AND f.uid IS NULL ]

2 个答案:

答案 0 :(得分:0)

要使用其他数据库配置组,请将name或config对象传递给execute()。

$ result = $ query-> execute('config_name')

http://kohanaframework.org/3.0/guide/database/query/builder#executing http://kohanaframework.org/3.0/guide/database/config#connection-settings

答案 1 :(得分:0)

这不是Kohana特定的问题。这是一个MySQL问题。

要在同一查询中对多个数据库进行操作,必须使用表名及其数据库名。如:

SELECT * FROM database1.table1 WHERE database1.table1.id IN (SELECT table1_id FROM database2.table2)

小心:通过字面编写查询,我不确定Kohana的查询构建器是否会像使用查询构建器的特定函数构建SQL查询那样阻止SQL注入攻击。我会使用函数mysql_real_escape_string

来阻止它