codeigniter - this->db->get() returns empty result - codeigniter

I have a problem regarding codeigniters db->get(). I have functioning sql query, when i pass it to db "manually", i get result, but when i use codeigniter, i get empty result. Almost same query but with like instead of where works.
The SQL is:
SELECT se.Id, se.Name, se.Alias, sy.Name as System, os.Name as Os, ac.Name as Access, se.Description
FROM cdl_server se
INNER JOIN cdl_system sy on sy.Id = se.System
INNER JOIN cdl_os os on os.Id = se.Os
INNER JOIN cdl_access ac on ac.Id = se.Access
WHERE se.Id = 1
The code looks like this:
$this->db->select(
'se.Id, se.Name, se.Alias, ' .
'sy.Name as System, os.Name as Os, ac.Name as Access, ' .
'se.Description',
FALSE);
$this->db->from('cdl_server se');
$this->db->join('cdl_system sy', 'sy.Id = se.System' ,'inner', FALSE);
$this->db->join('cdl_os os', 'os.Id = se.Os' ,'inner', FALSE);
$this->db->join('cdl_access ac', 'ac.Id = se.Access' ,'inner', FALSE);
$this->db->where('se.Id', $id, FALSE);
$this->db->get();
edit:
I use it like:
$query = $this->db->get();
return $query->result();
When i var_dump the sql query before it is sent to the database, i get this:
string(264) "SELECT se.Id, se.Name, se.Alias, sy.Name as System, os.Name as Os, ac.Name as Access, se.Description FROM cdl_server se INNER JOIN cdl_system sy ON sy.Id = se.System INNER JOIN cdl_os os ON os.Id = se.Os INNER JOIN cdl_access ac ON ac.Id = se.Access WHERE se.Id =1"
But result is always empty, even though when i copy the above and try asking db directly, i get result i need.
Does anyone know what could be causing it?

If u want to get result you must use row() or result() function.
$this->db->get()->result();

Related

Oracle Query not giving result though outer join is present

In the below query when i comment one line AND EVT.EVT_EVNT_ID(+) = DPT.DPT_EVNT_ID or add outer join (+) in one line AND EVT.EVT_ENTITY_ID ='G' the query will fetch results, else it will not give any results.
`
select * from
(select B11.ext_db_id,B11.FRST_LINE_OF_PRFRD_NAME, E11.*, D1.D1_evnt_id,
(SELECT E1.E1_DT
FROM S1core.E1_EVNT_DT_DTLS E1
WHERE E1.E1_ENTITY_ID = D1.D1_ENTITY_ID
AND E1.E1_EVNT_ID = D1.D1_EVNT_ID
AND E1.E1_D1_ID = D1.D1_D1_ID
AND E1.E1_DT_TYP = 'MEET'
AND E1.E1_OPTN_SEQ_N = '999'
AND E1.STATUS = 'AUTHD' ) "MEET_DATE"
FROM S1core.E1_EVNT_DT_DTLS E11, S1core.SECURITY_ACCOUNT SFA,
S1core.EXTRNL_SYS_DETAILS EXT,
S1core.D1_DPOT_EVNT_DTLS D1, S1core.IP_ACNT_RELATION i1,
S1core.ELG_ELGBLTY E1,S1core.P1_D1_PRXY_DTLS P1, S1core.EVT_DTLS EVT,
S1core.BUSINESS_PARTNER B1, S1core.BUSINESS_PARTNER B11
WHERE
P1.P1_EVNT_ID(+) = D1.D1_EVNT_ID
AND B1.BP_ID = i1.IP_ID
AND SFA.BP_ID = B11.BP_ID
AND B11.OWNER_ENTITY = SFA.OWNER_ENTITY
AND P1.P1_D1_ID(+) = D1.D1_D1_ID
AND P1.P1_ENTITY_ID(+) = D1.D1_ENTITY_ID
AND P1.STATUS(+) = 'AUTHD'
AND EXT.LVL_REF = SFA.SCA_REF
AND EXT.OWNER_ENTITY = SFA.owner_entity
AND EXT.EXTRNL_SYS_ID = '39'
AND EXT.BP_ID = SFA.BP_ID
AND EXT.LVL = 5
AND E1.ELG_SEC_ID = D1.D1_SEC_ID
AND D1.D1_ENTITY_ID = E1.ELG_ENTITY_ID
AND D1.D1_EVNT_ID = E1.ELG_EVNT_ID
AND D1.D1_D1_ID = E1.ELG_D1_ID
AND E1.ELG_ENTITY_ID = SFA.OWNER_ENTITY
AND E1.ELG_ACNT_ID = SFA.SCA_REF
AND i1.owner_entity = SFA.OWNER_ENTITY
AND i1.owner_entity = B1.OWNER_ENTITY
AND i1.SCA_REF = SFA.SCA_REF
AND i1.stat <> 2
AND EVT.EVT_ENTITY_ID ='G'
AND EVT.EVT_EVNT_ID(+) = D1.D1_EVNT_ID
AND EVT.STATUS(+) = 'AUTHD'
AND E11.E1_ENTITY_ID = D1.D1_ENTITY_ID
AND E11.E1_EVNT_ID = D1.D1_EVNT_ID
AND E11.E1_D1_ID = D1.D1_D1_ID
AND D1.D1_EVNT_GRP = 'MEETING'
AND E1.ELG_FNL_ELGBL_QNTTY > 0
AND D1.D1_ENTITY_ID = 'GSSIN' -- P_D1_ENTITY_ID
AND B11.ext_db_id LIKE 'LICMF' -- P_GLOBAL_CUSTODIAN
AND B1.ext_db_id LIKE '%' -- P_FUND_MANAGER
AND SUBSTR(EXT.LEVEL_EXTNL_SYS,1,5) LIKE '%' -- P_SUB_ACCOUNT_ID
AND SUBSTR(EXT.LEVEL_EXTNL_SYS,6,9) LIKE '%' -- P_SCHEMA_ID
AND SUBSTR( D1.D1_EVNT_TYP, 1, 4 ) LIKE '%' -- P_EVENT_TYPE
AND E11.E1_DT_TYP = 'MEET'
and D1.D1_evnt_id='12457886544'
AND D1.OU_ID IN('17','80') -- ('S1 INDIA PC')
) ;
Am I missing any outer join or the query is having any other issue as i can see that dpt_evnt_id='12457886544'is available in all tables.
`
I tried to run this query by commenting AND EVT.EVT_EVNT_ID(+) = DPT.DPT_EVNT_ID
and by adding outer join in EVT.EVT_ENTITY_ID (+) ='G'
You have:
AND EVT.EVT_ENTITY_ID ='G'
AND EVT.EVT_EVNT_ID(+) = D1.D1_EVNT_ID
AND EVT.STATUS(+) = 'AUTHD'
Even though the second two have (+) the first one does not so the join to EVT will be treated as an INNER JOIN and not an OUTER JOIN.
You should convert your code to use ANSI joins as it will be much easier to spot issues like this.
Note: There may be more issues like this. However, your code has so many joins, the syntax is archaic, and we have no way of validating the correctness that I stopped looking after finding the first issue. You are going to need to debug it all to check for any other issues.

Eloquent: Complex Script

Good day!
can be possible to change this script to Eloquent Laravel
SELECT concat(firstname, " ",lastname) as fullname,q_title,answer FROM `user_pivot_survey_answer` as upsa
LEFT JOIN (SELECT id,q_title FROM survey_question) sq ON upsa.qid = sq.id
LEFT JOIN (SELECT id,firstname,lastname FROM user_survey_answer) usa ON upsa.sid = usa.id
I translate it already to Eloquent, however, I don't know if is working
$testquery = DB::table('user_pivot_survey_answer')
->leftJoin(DB::select('SELECT id,q_title FROM survey_question'),function($join)) {
$join->on('user_pivot_survey_answer.id', '=', 'survey_question.id');
})
->leftJoin(DB::select('SELECT id,firstname,lastname FROM user_survey_answer'),function($join){
$join->on('user_pivot_survey_answer.id', '=', 'user_survey_answer.id');
});
Thanks
Use this:
$testquery = DB::table('user_pivot_survey_answer as upsa')
->select(DB::raw('concat(firstname, " ", lastname) as fullname'), 'q_title', 'answer')
->leftJoin(DB::raw('(SELECT id,q_title FROM survey_question) sq'), 'upsa.qid', 'sq.id')
->leftJoin(DB::raw('(SELECT id,firstname,lastname FROM user_survey_answer) usa'), 'upsa.sid', 'usa.id');

Leading 0 in serach box throws error (Laravel Datatable)

I have used left to join multiple tables in my controller method. It returns data as desired but when I type into search box '0' it throws an error.
Using https://datatables.yajrabox.com/ this Laravel Datatable plugin
It only happen when single zero(0) is there in search box. '00' or 't0102' works fine.
only single '0' as search value throws an error.
Happens on the demo site to
https://datatables.yajrabox.com/eloquent/row-num
try to search for 0(zero) only and it will throw back you an error.
After spending couple of hours I came to the conclusion the it is because in MySql query LIKE %0% is not adding inverted commas('') around 0(zero)
It should be LIKE '%0%' instead LIKE %0%.
Code snippet of problem
Below is my Query Builder Code
$surveys = DB::table('surveys as su')
->leftJoin('assignments as a', 'su.assignment_id', '=', 'a.id')
->leftJoin('rounds as r', 'a.round_id', '=', 'r.id')
->leftJoin('projects as p', 'r.project_id', '=', 'p.id')
->leftJoin('sites as s', 'a.site_id', '=', 's.id')
->leftJoin('fieldreps as f', 'a.fieldrep_id', '=', 'f.id')
->leftJoin('chains as ch','p.chain_id','=','ch.id')
->leftJoin('clients as c','ch.client_id','=','c.id')
->leftJoin('surveys_templates as t', 'su.template_id', '=', 't.id')
->where(function ($query) {
$query->where('a.is_reported', '=', true)
->orWhere('a.is_partial', '=', true)
->orWhere('a.is_approved', '=', true);
})
//->where('su.service_code', '!=', '')
->select([
'a.id as assignment_id',
'a.fieldrep_id',
'a.deadline_date',
'a.is_reported',
'a.is_partial',
'a.is_approved',
'c.client_logo',
'p.id as project_id',
'p.project_name',
'r.id as round_id',
'r.round_name',
's.site_code',
's.site_name',
's.city',
's.state',
's.zipcode',
'su.id as survey_id',
'su.status',
'su.service_code',
't.template_name',
DB::raw("CONCAT(IFNULL( DATE_FORMAT(a.schedule_date,'%e/%c/%Y'), DATE_FORMAT(r.schedule_date,'%e/%c/%Y')), ' ' , IFNULL(a.start_time, r.start_time)) as assignment_scheduled"),
DB::raw("CONCAT(IFNULL( DATE_FORMAT(a.schedule_date,'%d %b %Y'), DATE_FORMAT(r.schedule_date,'%d %b %Y')), ' ' , IFNULL(a.start_time, r.start_time)) as assignment_scheduled_date"),
DB::raw("CONCAT(DATE_FORMAT(a.start_date,'%d %b %Y'),' ',a.start_time) as assignment_starts"),
DB::raw('CONCAT(f.first_name," ",f.last_name) as schedule_to'),
]);
Syntax error or access violation: 1583 Incorrect parameters in the call to native function 'LOWER' (SQL: select count(*) as aggregate from (select a.id as assignment_id, a.fieldrep_id, a.deadline_date, a.is_reported, a.is_partial, a.is_approved, c.client_logo, p.id as project_id, p.project_name, r.id as round_id, r.round_name, s.site_code, s.site_name, s.city, s.state, s.zipcode, su.id as survey_id, su.status, su.service_code, t.template_name, CONCAT(IFNULL( DATE_FORMAT(a.schedule_date,'%e/%c/%Y'), DATE_FORMAT(r.schedule_date,'%e/%c/%Y')), ' ' , IFNULL(a.start_time, r.start_time)) as assignment_scheduled, CONCAT(IFNULL( DATE_FORMAT(a.schedule_date,'%d %b %Y'), DATE_FORMAT(r.schedule_date,'%d %b %Y')), ' ' , IFNULL(a.start_time, r.start_time)) as assignment_scheduled_date, CONCAT(DATE_FORMAT(a.start_date,'%d %b %Y'),' ',a.start_time) as assignment_starts, CONCAT(f.first_name," ",f.last_name) as schedule_to from surveys as su left join assignments as a on su.assignment_id = a.id left join rounds as r on a.round_id = r.id left join projects as p on r.project_id = p.id left join sites as s on a.site_id = s.id left join fieldreps as f on a.fieldrep_id = f.id left join chains as ch on p.chain_id = ch.id left join clients as c on ch.client_id = c.id left join surveys_templates as t on su.template_id = t.id where (a.is_reported = 1 or a.is_partial = 1 or a.is_approved = 1) and (LOWER(s.site_code) LIKE %0% or LOWER(su.service_code) LIKE %0% or LOWER(p.project_name) LIKE %0% or LOWER(r.round_name) LIKE %0% or LOWER(s.city) LIKE %0% or LOWER(surveys as su.assignment_scheduled) LIKE %0% or LOWER(t.template_name) LIKE %0% or LOWER(surveys as su.schedule_to) LIKE %0%) order by lpad(s.site_code, 10, 0) desc) count_row_table)'

1 result set from 3 queries?

I'm at my wits end attempting to make this happen. Currently I have 3 separate queries. For automation purposes I need to get the results of these three queries in one output, I cannot seem to join them properly to get the expected results.
QUERY 1:
SELECT OH.EXTN_HOST_ORDER_REF,
OL.EXTN_HOST_ORDER_LINE_REF,
OL.ORIGINAL_ORDERED_QTY,
OL.EXTN_TENDER_QUANTITY,
OL.EXTN_CUM_PICK_QTY,
OL.SHIPPED_QUANTITY,
OL.EXTN_REFUND_QTY
FROM YFS_ORDER_HEADER OH,
YFS_ORDER_LINE OL
WHERE OH.ORDER_HEADER_KEY = OL.ORDER_HEADER_KEY
AND OH.DOCUMENT_TYPE = '0001'
AND OH.EXTN_HOST_ORDER_REF = 'xxxxxxxxxxx'
ORDER BY PL.EXTN_HOST_ORDER_LINE_REF ASC;
QUERY 2:
SELECT RS.STATUS_QUANTITY AS RETURNED_QTY
FROM YFS_ORDER_HEADER OH,
YFS_ORDER_LINE OL,
YFS_ORDER_RELEASE_STATUS RS
WHERE OH.ORDER_HEADER_KEY = OL.ORDER_HEADER_KEY
AND OL.ORDER_LINE_KEY = RS.ORDER_LINE_KEY
AND RS.STATUS = '3700.02'
AND OH.EXTN_HOST_ORDER_REF = 'xxxxxxxxxxx';
QUERY 3
SELECT RS.STATUS_QUANTITY AS CANCELLED_QTY
FROM YFS_ORDER_HEADER OH,
YFS_ORDER_LINE OL,
YFS_ORDER_RELEASE_STATUS RS
WHERE OH.ORDER_HEADER_KEY = OL.ORDER_HEADER_KEY
AND OL.ORDER_LINE_KEY = RS.ORDER_LINE_KEY
AND RS.STATUS = '9000'
AND OH.EXTN_HOST_ORDER_REF = 'xxxxxxxxxxx';
The query should show NULL values where no data exists from query 2 & 3.
Thanks in advance for your help and advice!
If you are allowed to show both returned and cancelled quantities, the following simple edit should work. Hope this helps.
SELECT oh.extn_host_order_ref,
ol.extn_host_order_line_ref,
ol.original_ordered_qty,
ol.extn_tender_quantity,
ol.extn_cum_pick_qty,
ol.shipped_quantity,
ol.extn_refund_qty,
DECODE (rs.status, '3700.02', rs.status_quantity) AS returned_qty,
DECODE (rs.status, '9000', rs.status_quantity) AS cancelled_qty
FROM yfs_order_header oh
INNER JOIN yfs_order_line ol
ON oh.order_header_key = ol.order_header_key
LEFT OUTER JOIN yfs_order_release_status rs
ON ol.order_line_key = rs.order_line_key
WHERE oh.document_type = '0001' AND oh.extn_host_order_ref = 'xxxxxxxxxxx'
ORDER BY pl.extn_host_order_line_ref ASC;

Syntax Error: Token Join at Position 760 when importing SQL into Discoverer 11g

I wrote the query (below) in PL/SQL Developer for a client and would like to import it into Oracle Discoverer Desktop 11g so the report is self-serve. The query works perfectly in PL/SQL but when I go to import it into Discoverer, I get the following errors: "Error: Syntax error: token JOIN at position 760. Error: Parse stopped due to fatal errors."
Can anyone tell me what these errors mean and how to resolve the issue?
Thanks!
Query:
Select eid.person_code, eid.last_name plnownr_lname, eid.first_name plnownr_fname, eid.birth_date plnownr_DOB, des.status_code, dun.unit_code, ect.last_name BenRecip_LNAME, ect.first_name BenRecip_fname, ect.birth_date BenRecip_DOB,decode(ect.relation,'00','Unknown','01','Spouse','02','Common-Law Spouse','03','Partner', '05','Daughter','06','Son','07','Step-Daughter','08','Step-Son','09', 'Granddaughter','11', 'Father','12','Mother', '20','Sister','21','Brother','24','Aunt','28','Nephew','29','Ex-spouse','31','Friend','43','Trust','50','User Defined','52','User Defined', '53','User Defined','54','User Defined',ect.relation) relation, bpn.description, bpn.plan_code, bend.premium_start_date, bend.premium_end_date
From p2k_hr_identities eid
Join p2k_hr_contacts ect on ect.eid_id = eid.id
Join p2k_be_benefit_recipients bbr on bbr.ect_id = ect.id
Join p2k_be_enrollments ben on ben.id = bbr.ben_id
Join p2k_be_plan_definitions bpn on bpn.id = ben.bpn_id
Join p2k_hr_employments eem on eem.eid_id = eid.id
and eem.termination_date is null
Join p2k_hr_assignments eas on eas.eem_id = eem.id
Join p2k_hr_assignment_details easd on easd.eas_id = eas.id
and easd.expiry = to_date('3999-12-31', 'yyyy-mm-dd')
Join p2k_cm_units dun on dun.id = easd.dun_id
Join p2k_be_enrollment_details bend on bend.ben_id = ben.id
and bend.premium_end_date = to_date('3999-12-31','yyyy-mm-dd')
and bend.premium_start_date != to_date('0001-01-01','yyyy-mm-dd')
Join p2k_cm_employment_statuses des on des.id = easd.des_id
and des.status_code not in ('TERMINATED', 'RETIRED')
Union
Select eid.person_code, eid.last_name plnownr_lname, eid.first_name eplnownr_fname, eid.birth_date plnownr_dob, des.status_code, dun.unit_code, eid.last_name BenRecip_lname, eid.first_name BenRecip_fname, eid.birth_date BenRecip_dob, 'Employee-NoRelation', bpn.description, bpn.plan_code, bend.premium_start_date, bend.premium_end_date
From p2k_hr_identities eid
Join p2k_hr_employments eem on eem.eid_id = eid.id
and eem.termination_date is null
Join p2k_be_enrollments ben on ben.eem_id = eem.id
Join p2k_be_plan_definitions bpn on bpn.id = ben.bpn_id
Join p2k_hr_assignments eas on eas.eem_id = eem.id
Join p2k_hr_assignment_details easd on easd.eas_id = eas.id
and easd.expiry = to_date('3999-12-31', 'yyyy-mm-dd')
Join p2k_cm_units dun on dun.id = easd.dun_id
Join p2k_be_enrollment_details bend on bend.ben_id = ben.id
and bend.premium_end_date = to_date('3999-12-31','yyyy-mm-dd')
and bend.premium_start_date != to_date('0001-01-01','yyyy-mm-dd')
Join p2k_cm_employment_statuses des on des.id = easd.des_id
and des.status_code not in ('TERMINATED', 'RETIRED')
Order by 2,3,5,6

Resources