Kohana 3 - 查询生成器给出 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我无法立即判断该查询生成器出了什么问题,但是,为了调试目的请检查一下。
在数据库链上调用
execute()
后,试试这个。这将以纯 SQL 形式显示最后执行的查询。值得一看的是查询生成器生成的内容,以及它与您在 phpmyadmin 中使用的 SQL 有何不同。
如果其他方法都失败,只需使用普通查询方法即可。
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.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.
您会注意到,对
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 thewhere
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#exprAs 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.