2015-02-09 92 views
1

我碰到一个奇怪的问题,我有,它真的让我难住。我正在使用laravel来构建我的网站,并且我刚刚编写了脚本以充当搜索引擎。它被构建并运行。laravel查询不返回值 - 奇怪的pdo


我只是用$query是一个laravel器类运行$query->get()。这是查询的内容:


select * from (
    select `l`.*, round(
     d.distance_unit 
     * DEGREES(ACOS(COS(RADIANS(p.latitude)) 
     * COS(RADIANS(z.latitude)) 
     * COS(RADIANS(p.longitude - z.longitude)) 
     + SIN(RADIANS(p.latitude)) 
     * SIN(RADIANS(z.latitude)))), 
      2 
    ) AS distance, `d`.`radius` from `listings` as `l` 
    inner join `horses` as `x` on `x`.`id` = `l`.`listing_id` 
    inner join `suburbs` as `z` on `z`.`id` = `l`.`suburb_id` 
    inner join (select 50 as radius, 111.045 as distance_unit) as d on 1 = 1 
    inner join `suburbs` as `p` on `p`.`id` = 1 
    where (
     `z`.`latitude` between 
      ? 
      and 
      ? 
     and `z`.`longitude` between 
      ? 
      and 
      ? 
    ) 
) as sub 
where `distance` <= `radius` 
order by `created_at` desc 
limit 9 offset 0 

查询绑定:

[bindings] => Array 
    (
     [0] => Illuminate\Database\Query\Expression Object 
      (
       [value:protected] => p.latitude - (d.radius/d.distance_unit) 
      ) 

     [1] => Illuminate\Database\Query\Expression Object 
      (
       [value:protected] => p.latitude + (d.radius/d.distance_unit) 
      ) 

     [2] => Illuminate\Database\Query\Expression Object 
      (
       [value:protected] => p.longitude - (d.radius/(d.distance_unit * COS(RADIANS(p.latitude)))) 
      ) 

     [3] => Illuminate\Database\Query\Expression Object 
      (
       [value:protected] => p.longitude + (d.radius/(d.distance_unit * COS(RADIANS(p.latitude)))) 
      ) 

    ) 

即查询被laravel运行。当我打印的SQL。但我不能为我的生活找出为什么它不会返回一个值,当我在phpmyadmin中运行的SQL呢。 (我所做的只是将绑定添加到问号中,然后在phpmyadmin中运行)。我得出的结论是,PDO必须做些不同的事情或以不同的方式加入。我对这些值中的每一个都使用DB::raw()方法。

+1

尝试通过print_r(DB :: getQueryLog())打印查询请参阅生成的查询 – 2015-02-09 13:03:29

+0

laravel似乎并不打印确切的查询。以上内容是从'DB :: getQueryLog()'中检索的内容。它似乎只是单独显示查询和绑定,这是我生命中最大的痛苦。哈哈。我读了它。它因为** PDO **类。它将它作为查询发送到sql服务器并显然绑定。所以它基本上不可能确定已经运行的查询...:/ – 2015-02-09 13:06:04

+1

我会建议尝试**发条**。您需要安装[composer包](https://github.com/itsgoingd/clockwork)和[Chrome扩展程序](https://chrome.google.com/webstore/detail/clockwork/dmggabnehkmmfmdffgajcflpdjlnoemp) (这是我的建议)或[可嵌入的webapp版本](https://github.com/itsgoingd/clockwork-web)。它将允许你调试任何东西,包括你的SQL查询,并显示针对数据库运行的确切查询(而不是带有“?”的查询,其中绑定数据应该在哪里查询)。 – Bogdan 2015-02-09 13:23:55

回答

2

我设法通过为mysql服务器配置my.ini来找到我的问题。我转身general_log_file = "queries.log"[mysqld]。然后运行SET global general_log = 1;mysql


这表明我的查询存在问题。即使我使用的是DB::raw(),绑定也被插入引号中。所以我只需要想办法去除它们。

+0

我改变了'$ query-> whereBetween()'到'$ query-> whereRaw()'并且只写了主代码 – 2015-02-09 13:35:22

+0

考虑接受你自己的答案。 – sepehr 2016-11-29 15:55:02