1

从 phpmyadmin 运行时,以下查询按预期返回一行。

SELECT units .  * , locations .  *
FROM units, locations
WHERE units.id = '1'
AND units.location_id = locations.id
LIMIT 0 , 30 

但是当我尝试在 Kohana 3 中这样做时:

$unit = DB::select('units.*', 'locations.*')
->from('units', 'locations')
->where('units.id', '=', $id)->and_where('units.location_id', '=', 'locations.id')
->execute()->as_array();
var_dump($unit);

它打印

数组(0){}

我究竟做错了什么?

4

2 回答 2

5

我无法立即判断该查询生成器出了什么问题,但是,出于调试目的,请检查它。

调用execute()你的数据库链后,试试这个。

echo Database::instance()->last_query;

这将在普通 SQL 中显示最后执行的查询。值得看看查询生成器生成了什么,以及它与您在 phpmyadmin 中使用的 SQL 有何不同。

如果一切都失败了,只需使用普通查询方法。

$query = "SELECT units .  * , locations .  *
FROM units, locations
WHERE units.id = :id
AND units.location_id = locations.id
LIMIT 0 , 30 ";

$unit = Db::query(Database::SELECT, $query)
          ->bind(':id', (int) $id)
          ->execute()
          ->as_array();
于 2010-05-31T23:27:31.483 回答
0

您会注意到您的呼叫last_query返回(在该where部分中):

WHERE units.location_id = 'locations.id'传递给比较的值被引用为字符串,因此结果集为空。这可能会对您有所帮助:http: //kohanaframework.org/guide/api/DB#expr

至于您的致命错误,请仔细检查以确保您明确传递了 $id (而不是另一个变量的引用副本),考虑到您没有提供任何来源,这就是我所能想到的。

于 2010-06-06T02:33:34.987 回答