Kohana 3 - 查询生成器给出 0 行

发布于 2024-09-04 01:30:21 字数 541 浏览 12 评论 0原文

从 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) { }

我做错了什么?

The following query returns one row as expected when run from phpmyadmin.

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

But when I try to do it in 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);

It prints

array(0) { }

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

呆头 2024-09-11 01:30:21

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

在数据库链上调用 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();

I can't immediately tell what is wrong with that query builder, however, checkout this for debugging purposes.

After calling execute() on your db chain, try this.

echo Database::instance()->last_query;

This will show in plain SQL the last query performed. It will be worth looking at what the query builder generated, and how it differs to your SQL you used in phpmyadmin.

If all else fails, just use the plain query methods.

$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();
把昨日还给我 2024-09-11 01:30:21

您会注意到,对 last_query 的调用返回了(在 where 部分):

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

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

You'll notice that your call to last_query returned (in the where portion):

WHERE units.location_id = 'locations.id' the value being passed for comparison is being quoted as a string, hence an empty result set. This may help you: http://kohanaframework.org/guide/api/DB#expr

As for your fatal error, double check to be sure you are passing $id explicitly (and not a referenced copy of another variable), that's all I could think of considering you aren't providing any source.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文