Mysql结果返回问题
我正在 FuelPHP 中使用 php + mysql 创建一个项目。 我有这个数据库查询(我没有使用他们内置的查询生成器,因为它对于复杂查询来说只是一个痛苦)。
$sql = DB::query('
SELECT game_id, lati, longi,
acos(sin (' . $lat . ')
* sin (radians( lati ))
+ cos (' . $lat . ')
* cos (radians(lati))
* cos (radians(longi) - ' . $lon . '))
*' . $R . ' AS D
FROM (
SELECT game_id, lati, longi
FROM games
WHERE lati > ' . $minLat .
' AND lati <' . $maxLat .
' AND longi > ' . $minLon .
' AND longi < ' . $maxLon .
') AS firstcut
WHERE
acos(sin (' . $lat . ')
* sin (radians( lati ))
+ cos (' . $lat . ')
* cos (radians(lati))
* cos (radians(longi) - ' . $lon . '))
*' . $R . '<' . $rad .
' ORDER BY D');
如果我执行这个并 print_r(result) ,则显示行数,即 2。
但是我无法处理或将此结果转换为数组,所以我不能
foreach($result as $row) { echo $row['id'] . ' + ' . $row['D']}
或类似的东西。
如果我粘贴此代码并将 php 变量替换为实际值到 phpmyadmin,我会收到绿色的“查询已成功执行”消息,但不会返回任何行(应返回两行,甚至没有“此查询返回零”行的消息)。 我对这种嵌套/复杂的 SQL 查询没有太多经验,因此任何帮助将不胜感激。
谢谢。
I'm creating a project in FuelPHP, with php + mysql.
I have this database query (I'm not using their built in query builder because its just a pain in the ass for complex queries).
$sql = DB::query('
SELECT game_id, lati, longi,
acos(sin (' . $lat . ')
* sin (radians( lati ))
+ cos (' . $lat . ')
* cos (radians(lati))
* cos (radians(longi) - ' . $lon . '))
*' . $R . ' AS D
FROM (
SELECT game_id, lati, longi
FROM games
WHERE lati > ' . $minLat .
' AND lati <' . $maxLat .
' AND longi > ' . $minLon .
' AND longi < ' . $maxLon .
') AS firstcut
WHERE
acos(sin (' . $lat . ')
* sin (radians( lati ))
+ cos (' . $lat . ')
* cos (radians(lati))
* cos (radians(longi) - ' . $lon . '))
*' . $R . '<' . $rad .
' ORDER BY D');
If I execute this and print_r(result) the number of rows is displayed i.e 2.
However I cant treat, or convert this result into an array, so I cant
foreach($result as $row) { echo $row['id'] . ' + ' . $row['D']}
Or something similar.
If I paste this code and replace the php variables with real values into phpmyadmin, I get the green 'The query was executed successfully' message but no rows are returned (two rows should be returned, there is not even a 'This query returned zero rows' message).
I dont have much experience with this kind of nested/complex sql queries, so any help would be appreciated.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要将
DB::SELECT
作为DB::query()
的第二个参数传递。You need to pass
DB::SELECT
as the second param forDB::query()
.您可能想要使用数据库结果中的 as_array 方法。或者看看这里: http://fuelphp.com/docs/classes/database /usage.html#结果
You might want to use the as_array method from the database result. Or take a look here: http://fuelphp.com/docs/classes/database/usage.html#results