I have a query that gets the schedule of a employee and display it in table. I sampled some employees with the default schedules but when I try to retrieve others that have different schedule, it always returns false meaning the table is empty. Here is the query that I made:
function get_Sked($id,$from,$to)
{
$query = "SELECT c.dt, a.TimeFrom,a.TimeTo FROM schedule a LEFT JOIN cal c ON a.SkedDate = c.dt AND a.EmpID = ? WHERE c.dt BETWEEN DATE(?) AND DATE(?) GROUP BY c.dt ORDER BY c.dt ASC";
$query = $this->db->query($query,array($id,$from,$to));
if($query->num_rows() == 0)
{
return false;
}
else
{
return $query->result_array();
}
}
and this is the method that calls it:
$data['sked'] = $this->DBmodel->get_Sked($id,$dFrom,$dTo);
if($data['sked'] == false)
{
echo "Default Schedule of 8:00 am to 5:00 pm."; //if table is empty show default sched.
var_dump($data['sked']);
}
else
{
foreach($data['sked'] as $val) //this will show the content of the table.
I am using the same query sequence as the others and not having problems only this one.....
I change my query structure to this:
$this->db->select('c.dt,a.TimeFrom,a.TimeTo');
$this->db->from('cal c');
$this->db->join('schedule a','a.SkedDate = c.dt');
$this->db->where('a.EmpID',$id);
$this->db->where('c.dt',$from);
$this->db->where('c.dt',$to);
$query = $this->db->get();
but returns false still....
Related
How to check if a column with specific ID exists in a joined table and store the result in variable?
I have this model in codeigniter with d.* several results and I need to add one extra variable "response = 0 or 1" to get from joining.
function getAllActive($placeid = false, $response = false)
{
$query = $this->db->select("d.* ");
//QUESTION SECTION
if($reponse){
$query = $query->join($this->table_demand_menu.' dm', 'dm.fk_demand = d.id', 'left', FALSE);
$query = $query->where("dm.fk_demand", 'd.id');
**//WE JOIN WHERE ROWS WITH DEMAND IDs EXIST - ofc they will for many cases**
// QUESTION!!! BUT IF THE ROW HAS A COLUMN WITH PLACEID - used 'having' as example
$query = $query->having("dm.restaurant_id ==". $placeid);
//IF TRUE RETURN IT AS 1 IF FALSE RETURN IT AS 0 and spit it out in a variable AS reponse
}
$query = $query->group_by("d.id");
$query = $query->get($this->table_name.' d');
return $query->result();
}
if($response){
$query = $query->select(" (SELECT COUNT(*) FROM ".$this->table_demand_menu." dm WHERE dm.restaurant_id = ".$id." AND dm.fk_demand = d.id) AS response");
}
I am calling a function from my IOS app which calls the function in PHP file which then should return the data retrieved, but it is return nil since I added a new part to my query.
My new query is working in the MYSQL tester service on my DB, but strangely is returning nil in my app.
Here is how I have it all set up in my PHP file:
function fetchAssocStatement($stmt)
{
if($stmt->num_rows>0)
{
$result = array();
$md = $stmt->result_metadata();
$params = array();
while($field = $md->fetch_field()) {
$params[] = &$result[$field->name];
}
call_user_func_array(array($stmt, 'bind_result'), $params);
if($stmt->fetch())
return $result;
}
return null;
}
// Select all posts + user information made by user with relevant $id
public function selectPosts($id) {
// declare array to store selected information
$returnArray = array();
/*
// sql JOIN
$sql = "SELECT Posts.id,
Posts.uuid,
Posts.caption,
Posts.path,
Posts.date,
Posts.imageHeight,
USERS.id,
USERS.username,
USERS.fullname,
USERS.profileImage
FROM Posts JOIN USERS ON
Posts.id = $id AND USERS.id = $id ORDER by date DESC
LIMIT 0, 5";
*/
$sql = "SELECT Posts.id,
Posts.uuid,
Posts.caption,
Posts.path,
Posts.date,
Posts.imageHeight,
USERS.id,
USERS.username,
USERS.fullname,
USERS.profileImage,
coalesce(A.LikeCNT,0)
FROM Posts INNER JOIN USERS ON
Posts.id = $id AND USERS.id = $id
LEFT JOIN (SELECT COUNT(A.uuidPost) LikeCNT, A.UUIDPost
FROM Activity A
WHERE type = ""
GROUP BY A.UUIDPOST) A
on A.UUIDPost = Posts.uuid
ORDER BY date DESC
LIMIT 0, 5";
if($stmt = $this->connection->prepare($sql))
{
$stmt->execute();
$stmt->store_result();
while($row = $this->fetchAssocStatement($stmt))
{
$returnArray[] = $row;
}
//$stmt->close();
}
return $returnArray;
}
As you'll be able to see I have a commented out query, which was my previous query for getting the posts(which worked great). But now that I am trying to get the "like count" for all the images, in my app it is return nothing...
If anyone can see one, please let me know!
Thanks in advance!!
This:
Posts.id = $id AND USERS.id = $id
That's a totally wonky join condition. You want posts and users who happen to share the exact same ID, which is HIGHLY unlikely to ever occur.
No idea on your actual table structure, but you probably want something more like
posts.user_id = users.id
...
where users.id = $id
You need to trap for errors. I think the SQL being executed is throwing an error and you're not trapping it; which is why no records are being returned. The type = "" seems to be an error in my mind. Either you want type is null or type = '' (empty set) but not type = "" or maybe you want type = 'somevalue' or no value for type at all. (eliminate the where clause entirely) I'd start there (eliminate where clause) and if you get results then you know the problem is with the where clause
I am confuse with this problem in mysql, I have two table, "A" and "B"
TableA:
S.No contact1 contact2 status
1 Blbh eeee 1
TAbleB:
S.No Phone1 phone2
1 ddd ssss
From this table i am going to get value, from TableA ia m going to check
if (status == 1)
{
run tableA;
}
else
{
run table b;
}
I am gone a return result of this query. In view, how to get value with respected column name. I have no idea of this, help me to get value in view.
public function contDetails($id){
$check = $this->db->query("SELECT contact_status FROM account WHERE id = '$id' ");
$str = $check->row();
$chk = $str->contact_status;
if($chk == 1){
$query = $this->db->query("SELECT * FROM account WHERE id = '$id'");
}else{
$query = $this->db->query("SELECT * FROM contact_details WHERE user_id = '$id'");
}
$run = $query->num_rows();
print_r($run);
}
You can use in your model
$query = $this->db->get(); //--- run the query ---//
return $query->result() //--- to get result in array of object ---//
and then in your view use foreach loop
foreach($results as $result){
echo $result->columnName;
}
have you already written the query in mysql? If yes and if you are concerned about whether the column name to use exist or not you can use isset...
in your view you can use like the following:
<?php
foreach($results as $result)
{
echo (isset($result['col1']))?$result['col1']:$result['col1_2'];
}
?>
And don't forget to use result_array() instsead of result in the controller
My code is
public function getbonexpense($yr=null)
{
$this->db->select('year(un_due_date) as year,month(un_due_date) as month,sum(coalesce(bank_amount,0) +amount) as bonsum');
$this->db->from('bon_expense');
$this->db->join('bank_expense','bon_expense.id_bon_exp = bank_expense.bon_exp_id', 'left');
$this->db->where('expense_status',3);
$this->db->group_by('year(un_due_date)');
$this->db->group_by('month(un_due_date)');
if(!empty($yr))$this->db->where('year(un_due_date)',$yr);
$this->db->order_by('month(un_due_date)','desc');
$query = $this->db->get();
if($query->num_rows() != 0)
return $query->result_array();
else
return false;
}
but automatically insert codes(`) in running query
SELECT year(un_due_date) as year, month(un_due_date) as month, sum(coalesce(amount, 0)+coalesce(bank_amount, **`0))`** as bonsum
FROM (`crm_bon_expense`)
LEFT JOIN `crm_bank_expense` ON `crm_bon_expense`.`id_bon_exp` = `crm_bank_expense`.`bon_exp_id`
WHERE `expense_status` = 3
AND year(un_due_date) = '2014'
GROUP BY year(un_due_date), month(un_due_date)
ORDER BY month(un_due_date) desc
The single backticks comes in the sum () fuction.How can i avoid it?
add 2nd param FALSE in db -> select method, see below sample code
$this->db->select('year(un_due_date) as year,
month(un_due_date) as month,
sum(coalesce(bank_amount,0) +amount) as bonsum', FALSE);
Documentation:
https://ellislab.com/codeigniter/user-guide/database/active_record.html#select
Dear all friends i am new in Codeigniter framework i want total amount by selecting two table data, the problem is that when i enter data in 2nd table than total will double.
function total_amount($booking_no = NULL)
{
$data = array('forwarding_cargo_booking_details.*',
'other_charges.client_amount');
$this->db->select($data);
$this->db->where('forwarding_cargo_booking_details.booking_no',$booking_no);
$this->db->join('other_charges','forwarding_cargo_booking_details.booking_no = other_charges.booking_no','left');
$this->db->select('sum((`bk_m3`*`o_freight_client`)*(`selling_rate`)+`pod_client`+`thc_client`+`caf_client`+`baf_client`+`haulage_client`+`war_risk_client`+`warehouse_client`+`thc_dest_client`+`pp_surcharge_client`+`doc_charges_client`+`client_amount`) as salam', FAlSE);
$query = $this->db->get('forwarding_cargo_booking_details');
if($query->num_rows() > 0)
{
return $query->row();
}
}
Change it to this. Here use derieved query and test if it works ok
function total_amount($booking_no = NULL)
{
$sql_query = "SELECT
forwarding_cargo_booking_details.*,
other_charges.client_amount,
sum((`bk_m3`*`o_freight_client`)*(`selling_rate`)+`pod_client`+`thc_client`+`caf_client`+`baf_client`+`haulage_client`+`war_risk_client`+`warehouse_client`+`thc_dest_client`+`pp_surcharge_client`+`doc_charges_client`+`client_amount`) as amount
FROM forwarding_cargo_booking_details
LEFT JOIN (SELECT booking_no , sum(client_amount) FROM other_charges group by booking_no) as other_charges ON forwarding_cargo_booking_details.booking_no = other_charges.booking_no
";
$query = $this->db->query();
if($query->num_rows() > 0)
{
return $query->row();
}
}
use this code to print the query, and check the query.
$this->db->last_query();
i think you forget to use "Group By" in your query