Запрос работает на сервере sql, не работает php codeigniter - PullRequest
0 голосов
/ 17 января 2019

Надеюсь, этот вопрос не является дубликатом.

У меня есть запрос, который работает на сервере SQL.Когда я пытаюсь использовать его в Codeigniter, я получаю Внутреннюю ошибку сервера (500).

Вот запрос с кодом php,

   public function tnaBucketTable($filter = null){
    $sql = "
        SELECT TOP 500 start ,SC.status ,S.shift_id ,SC.call_id,SC.type,SC.win_end,S.job_id
        ,pin = CASE 
                WHEN pin IS NULL THEN pin_agency 
                ELSE pin 
            END
            ,CONVERT(VARCHAR, start, 3)+' '+CONVERT(CHAR(5), start, 108) AS start_time
            ,CONVERT(CHAR(5), [end], 108) AS finish_time
            ,guard_name = CASE guard_info
                WHEN 'Agency' THEN  (SELECT shortname FROM [SecurityPlatform].[dbo].[SP-Guard-Agency] WHERE pin = S.pin_agency)
                ELSE (SELECT shortname FROM [SecurityPlatform].[dbo].[SP-Guard] WHERE pin = S.pin)
            END
            ,guard_number = CASE guard_info
                WHEN 'Agency' THEN  (SELECT phone1 FROM [SecurityPlatform].[dbo].[SP-Guard-Agency] WHERE pin = S.pin_agency)
                ELSE (SELECT phone_number FROM [SecurityPlatform].[dbo].[SP-Guard] WHERE pin = S.pin)
            END
            ,agency_number = CASE S.guard_info
                WHEN 'Agency' THEN (
                    SELECT contact_no_1 FROM [SecurityPlatform].[dbo].[SP-Agency] WHERE agency_id = (
                        SELECT agency_id FROM [SecurityPlatform].[dbo].[SP-Guard-Agency] WHERE pin = S.pin_agency
                    )
                )
                ELSE NULL
            END
            ,C.client_name
            ,ST.site_name
            ,ST.phone_number AS site_phone
            ,ST.site_sin
            FROM [SecurityPlatform].[dbo].[SP-Shift] S
            JOIN [SecurityPlatform].[dbo].[SP-Job] J ON J.job_id = S.job_id
            JOIN [SecurityPlatform].[dbo].[SP-Site] ST ON ST.site_sin = J.[sin]
            JOIN [SecurityPlatform].[dbo].[SP-Client] C ON C.client_id = ST.client_id
            JOIN [SecurityPlatform].[dbo].[SP-ScheduleCall] SC ON SC.shift_id = S.shift_id 
            JOIN [SecurityPlatform].[dbo].[SP-TrackingCall] TC ON TC.call_id = SC.call_id
            WHERE S.[status] = 'Confirmed'
            AND SC.[status] != 'Unconfirmed'
            AND pin > 0 ORDER BY S.shift_id DESC, SC.win_end DESC";

    $query = $this->db->query($sql);
    return $query->result();
}

И результат с сервера Sql:

result

Результат из браузера:

enter image description here

У всех есть идеи, чтоя пропал?

Пожалуйста, спросите, нужна ли вам дополнительная информация.

Заранее спасибо.

Редактировать: var_dump ($ sql) для запроса

string(1763) " SELECT TOP 500 start ,SC.status ,S.shift_id ,SC.call_id,SC.type,SC.win_end,S.job_id ,pin = CASE WHEN pin IS NULL THEN pin_agency ELSE pin END ,CONVERT(VARCHAR, start, 3)+' '+CONVERT(CHAR(5), start, 108) AS start_time ,CONVERT(CHAR(5), [end], 108) AS finish_time ,guard_name = CASE guard_info WHEN 'Agency' THEN (SELECT shortname FROM [SecurityPlatform].[dbo].[SP-Guard-Agency] WHERE pin = S.pin_agency) ELSE (SELECT shortname FROM [SecurityPlatform].[dbo].[SP-Guard] WHERE pin = S.pin) END ,guard_number = CASE guard_info WHEN 'Agency' THEN (SELECT phone1 FROM [SecurityPlatform].[dbo].[SP-Guard-Agency] WHERE pin = S.pin_agency) ELSE (SELECT phone_number FROM [SecurityPlatform].[dbo].[SP-Guard] WHERE pin = S.pin) END ,agency_number = CASE S.guard_info WHEN 'Agency' THEN ( SELECT contact_no_1 FROM [SecurityPlatform].[dbo].[SP-Agency] WHERE agency_id = ( SELECT agency_id FROM [SecurityPlatform].[dbo].[SP-Guard-Agency] WHERE pin = S.pin_agency ) ) ELSE NULL END ,C.client_name ,ST.site_name ,ST.phone_number AS site_phone ,ST.site_sin FROM [SecurityPlatform].[dbo].[SP-Shift] S JOIN [SecurityPlatform].[dbo].[SP-Job] J ON J.job_id = S.job_id JOIN [SecurityPlatform].[dbo].[SP-Site] ST ON ST.site_sin = J.[sin] JOIN [SecurityPlatform].[dbo].[SP-Client] C ON C.client_id = ST.client_id JOIN [SecurityPlatform].[dbo].[SP-ScheduleCall] SC ON SC.shift_id = S.shift_id JOIN [SecurityPlatform].[dbo].[SP-TrackingCall] TC ON TC.call_id = SC.call_id WHERE S.[status] = 'Confirmed' AND SC.[status] != 'Unconfirmed' AND pin > 0 ORDER BY S.shift_id DESC, SC.win_end DESC"

Ответы [ 2 ]

0 голосов
/ 18 января 2019

Я испробовал все рекомендации здесь, но не смог найти результат, поэтому я изменил свой запрос Sql на конструктор запросов Codeigniter, и теперь он работает без каких-либо временных ошибок.

Надеюсь, это кому-нибудь еще поможет.

$this->db->select("
    start,
    SC.status
    ,S.shift_id
    ,SC.call_id
    ,SC.type
    ,SC.win_end
    ,S.job_id
    ,pin =
    CASE 
        WHEN pin IS NULL THEN pin_agency 
        ELSE pin 
    END 
    ,CONVERT(VARCHAR, start, 3)+' '+CONVERT(CHAR(5), start, 108) AS start_time
    ,CONVERT(CHAR(5), [end], 108) AS finish_time
    ,guard_name = 
    CASE guard_info
        WHEN 'Agency' THEN  (SELECT shortname FROM [SecurityPlatform].[dbo].[SP-Guard-Agency] WHERE pin = S.pin_agency)
        ELSE (SELECT shortname FROM [SecurityPlatform].[dbo].[SP-Guard] WHERE pin = S.pin)
    END
    ,guard_number =
    CASE guard_info
        WHEN 'Agency' THEN  (SELECT phone1 FROM [SecurityPlatform].[dbo].[SP-Guard-Agency] WHERE pin = S.pin_agency)
        ELSE (SELECT phone_number FROM [SecurityPlatform].[dbo].[SP-Guard] WHERE pin = S.pin)
    END
    ,agency_number =
    CASE S.guard_info
        WHEN 'Agency' THEN (
            SELECT contact_no_1 FROM [SecurityPlatform].[dbo].[SP-Agency] WHERE agency_id = (
                SELECT agency_id FROM [SecurityPlatform].[dbo].[SP-Guard-Agency] WHERE pin = S.pin_agency
            )
        )
        ELSE NULL
    END
    ,C.client_name
    ,ST.site_name
    ,ST.phone_number AS site_phone
    ,ST.site_sin ", FALSE);

    $this->db->from('SP-Shift S');
    $this->db->join('SP-Job J', 'J.job_id = S.job_id');
    $this->db->join('SP-Site ST', 'ST.site_sin = J.sin');
    $this->db->join('SP-Client C', 'C.client_id = ST.client_id');
    $this->db->join('SP-ScheduleCall SC', 'SC.shift_id = S.shift_id');
    $this->db->join('SP-TrackingCall TC', 'TC.call_id = SC.call_id');
    $this->db->where('S.status', 'Confirmed');
    $this->db->where('SC.status != ', 'Unconfirmed');
    $this->db->where('pin >= ', 0);
0 голосов
/ 17 января 2019

Это

$query = "SELECT TOP 500 start,SC.status,S.shift_id,SC.call_id,SC.type,SC.win_end,S.job_id,pin = CASE 
            WHEN pin IS NULL THEN pin_agency 
            ELSE pin 
        END
        ,CONVERT(VARCHAR, start, 3)+' '+CONVERT(CHAR(5), start, 108) AS start_time
        ,CONVERT(CHAR(5), [end], 108) AS finish_time
        ,guard_name = CASE guard_info
            WHEN 'Agency' THEN  (SELECT shortname FROM [SecurityPlatform].[dbo].[SP-Guard-Agency] WHERE pin = S.pin_agency)
            ELSE (SELECT shortname FROM [SecurityPlatform].[dbo].[SP-Guard] WHERE pin = S.pin)
        END
        ,guard_number = CASE guard_info
            WHEN 'Agency' THEN  (SELECT phone1 FROM [SecurityPlatform].[dbo].[SP-Guard-Agency] WHERE pin = S.pin_agency)
            ELSE (SELECT phone_number FROM [SecurityPlatform].[dbo].[SP-Guard] WHERE pin = S.pin)
        END
        ,agency_number = CASE S.guard_info
            WHEN 'Agency' THEN (
                SELECT contact_no_1 FROM [SecurityPlatform].[dbo].[SP-Agency] WHERE agency_id = (
                    SELECT agency_id FROM [SecurityPlatform].[dbo].[SP-Guard-Agency] WHERE pin = S.pin_agency
                )
            )
            ELSE NULL
        END
        ,C.client_name
        ,ST.site_name
        ,ST.phone_number AS site_phone
        ,ST.site_sin
        FROM [SecurityPlatform].[dbo].[SP-Shift] S
        JOIN [SecurityPlatform].[dbo].[SP-Job] J ON J.job_id = S.job_id
        JOIN [SecurityPlatform].[dbo].[SP-Site] ST ON ST.site_sin = J.[sin]
        JOIN [SecurityPlatform].[dbo].[SP-Client] C ON C.client_id = ST.client_id
        JOIN [SecurityPlatform].[dbo].[SP-ScheduleCall] SC ON SC.shift_id = S.shift_id 
        JOIN [SecurityPlatform].[dbo].[SP-TrackingCall] TC ON TC.call_id = SC.call_id
        WHERE S.[status] = 'Confirmed'
        AND SC.[status] != 'Unconfirmed'
        AND pin > 0 ORDER BY S.shift_id DESC, SC.win_end DESC";
        var_dump($query);    

выводит это

SELECT TOP 500 start,SC.status,S.shift_id,SC.call_id,SC.type,SC.win_end,S.job_id,pin = CASE WHEN pin IS NULL THEN pin_agency ELSE pin END ,CONVERT(VARCHAR, start, 3)+' '+CONVERT(CHAR(5), start, 108) AS start_time ,CONVERT(CHAR(5), [end], 108) AS finish_time ,guard_name = CASE guard_info WHEN 'Agency' THEN (SELECT shortname FROM [SecurityPlatform].[dbo].[SP-Guard-Agency] WHERE pin = S.pin_agency) ELSE (SELECT shortname FROM [SecurityPlatform].[dbo].[SP-Guard] WHERE pin = S.pin) END ,guard_number = CASE guard_info WHEN 'Agency' THEN (SELECT phone1 FROM [SecurityPlatform].[dbo].[SP-Guard-Agency] WHERE pin = S.pin_agency) ELSE (SELECT phone_number FROM [SecurityPlatform].[dbo].[SP-Guard] WHERE pin = S.pin) END ,agency_number = CASE S.guard_info WHEN 'Agency' THEN ( SELECT contact_no_1 FROM [SecurityPlatform].[dbo].[SP-Agency] WHERE agency_id = ( SELECT agency_id FROM [SecurityPlatform].[dbo].[SP-Guard-Agency] WHERE pin = S.pin_agency ) ) ELSE NULL END ,C.client_name ,ST.site_name ,ST.phone_number AS site_phone ,ST.site_sin FROM [SecurityPlatform].[dbo].[SP-Shift] S JOIN [SecurityPlatform].[dbo].[SP-Job] J ON J.job_id = S.job_id JOIN [SecurityPlatform].[dbo].[SP-Site] ST ON ST.site_sin = J.[sin] JOIN [SecurityPlatform].[dbo].[SP-Client] C ON C.client_id = ST.client_id JOIN [SecurityPlatform].[dbo].[SP-ScheduleCall] SC ON SC.shift_id = S.shift_id JOIN [SecurityPlatform].[dbo].[SP-TrackingCall] TC ON TC.call_id = SC.call_id WHERE S.[status] = 'Confirmed' AND SC.[status] != 'Unconfirmed' AND pin > 0 ORDER BY S.shift_id DESC, SC.win_end DESC    

для меня. Можете ли вы попробовать вывод var_dump прямо в вашем SQL? Я думаю, у меня есть небольшая подсказка ...

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...