So basically I have a TIMESTAMPDIFF query in my model to determine the duration and I want it to display the duration in a view. The problem is, it will display the error.
Please help me, thank you.
Here's my query of my model (model_attendance.php):
public function getOne($username){
$sql = "SELECT empnum FROM employees WHERE username='$username'";
$result = $this->db->query($sql);
$sql1 = "SELECT a.empnum,CONCAT(a.name,' ',a.midname,' ',a.lastname) AS
NAME,CONCAT(b.indate,' ',b.intime) AS 'TIMEIN',CONCAT(b.outdate,'
',b.outtime)AS 'TIMEOUT', TIMESTAMPDIFF(HOUR, 'TIMEIN','TIMEOUT') AS 'DUR'
FROM employees AS a
JOIN times AS b ON (a.empnum=b.userid)
WHERE b.indate BETWEEN
DATE(CONCAT(YEAR(CURRENT_TIMESTAMP),'-',MONTH(CURRENT_TIMESTAMP),'-','01'))
AND DATE(LAST_DAY(CURRENT_TIMESTAMP)) AND b.userid='".$result->row(0)->empnum."'";
$query = $this->db->query($sql1);
return $query->result();
}
To solve this problem, you need to Pass return type as "false" to Active record select query.
$this->db->select("TIMESTAMPDIFF(YEAR,a.date_birth, CURDATE()) AS age", false);
Assign that to variable and pass that to your SQL Query.
$now = date('Y-m-d H:i:s'); # 2015-1-30 04:32:25 / With time
$now = date('Y-m-d'); # 2015-1-30 / Without time
Its potentially a bug. I Havent checked the Query Builder class yet. But to fix this...you can try by adding ` to the column like this
$this->db->select("TIMESTAMPDIFF(YEAR, `a.date_birth`, CURDATE()) AS age");
or
$this->db->query("TIMESTAMPDIFF(YEAR, `a.date_birth`, CURDATE()) AS age");
Related
I have set up cronjob wherein MySQL query there are used NOW SQL function at lots of time. I want to run cronjob for a past date so I need to change in code. But I think if I will change return static past date value in now function of SQL then I can do a thing without code change.
My current code in cronjob.
....
$query = "SELECT * from table_name where from_date <= DATE(NOW())";
....
You can set the system variable timestamp to set a time that is returned by NOW()
Why you need to override, Its not feasible? You can use past date instead of now as you specified in question!!
$query = "SELECT * from table_name where from_date <= 'yyyy-mm-dd H:i:S'";
$query = "SELECT * from table_name where from_date <= '2018-05-05 05:05:05'";
So this is odd and hopefully there is a simple answer. I have a SQL query that used a date as the first parameter in a where() clause. But for some reason CodeIginter appears to be trying to add in extra escape characters,
Here is my code
$this->db->select("*");
$this->db->from("equip_reserve");
$this->db->where('equip_list_id = '.$equip_list_id);
$this->db->where('"'. date("Y-m-d H:i",strtotime($startdate)) .'" between date and returndate');
Here is the result:
SELECT * FROM 'equip_reserve' WHERE 'equip_list_id' = 5 AND "2016-02-29 '14:20'" between 'date' and 'returndate'
As you can see the date after the AND clause is causing this statement to fail.
Any suggestions on how to fix it?
I ended up fixing it by just passing the SQL directly to CodeIgniter instead of using their helper functions. In case anyone ever has this problem.
$st = date("Y-m-d H:i",strtotime($startdate));
$sql = "SELECT * FROM `equip_reserve` WHERE ? between `date` AND `returndate` and `equip_list_id` = ?";
$query = $this->db->query(
$sql,
array($star,$equip_list_id)
);
return $query->result();
Fixed!
I know my question is similar to other question already answered but my issue is different because I need some alternative or advice no how to go the other way around.
My issue is: I want to get values between either two dates or one according to what user wants..
When User request data of one day.. php query data successful.. but problem is when data requested is between two dates..
$query = $this->db->query("SELECT * FROM `meta_receipt_data`
WHERE `meta_transaction_date` >= '$first_date' AND
`meta_transaction_date` <= '$second_date' ");
return $query->result();
I get an empty set...
So I thought may be the values are not submitted correct.. so I echoed the values to see if they are correct or not. I find they are correct...
$first_date = 09/13/2014;
$second_date = 09/19/2014;
But if I try to put the value like
$query = $this->db->query("SELECT * FROM `meta_receipt_data`
WHERE `meta_transaction_date` >= '09/13/2014' AND
`meta_transaction_date` <= '09/19/2014' ");
return $query->result();
I get my result back correct.. so is there anything am doing it wrong??
Change the type of meta_transaction_date to DATE as that is what it is! Also use the standard 'yyyy-mm-dd' when passing in DATEs.
Your problem probably stems from string ordering of the 'mm/dd/yyyy' US date format which is horrible for coding. If you wish to display the DATE in this format, convert it when SELECTing the final output.
MySQL has a built in function called Between that you can use like this:
SELECT * FROM table_name WHERE date_column BETWEEN 'start_date_parameter' AND 'end_time_parameter'
Try to cast the date first , and then with between statement:
$query = $this->db->query("SELECT * FROM `meta_receipt_data`
WHERE `meta_transaction_date` BETWEEN
date_format(str_to_date('$first_date', '%d/%m/%Y'), '%Y-%m-%d') AND
date_format(str_to_date('$second_date', '%d/%m/%Y'), '%Y-%m-%d')");
$query = $this->db->query("SELECT * FROM `meta_receipt_data`
WHERE `meta_transaction_date` >= '09/13/2014'
AND `meta_transaction_date` <= '09/19/2014' ");
Since the above seems to be working fine, the problem is in your code.
$query = $this->db->query("SELECT `meta_transaction_date` FROM meta_receipt_data WHERE
meta_transaction_date BETWEEN "
.date( "Y-M-d", ( strtotime($first_date) ) )." AND "
.date( "Y-M-d", ( strtotime($second_date) ) ) );
A word of advice, do not use queries like SELECT * as they will degrade performance of your application. And I just read in your comment to your own question:
I have set the type as Varchar
Do not do that. It is best to use the DATE type to store dates. You can use the query:
ALTER TABLE `meta_receipt_data`
MODIFY `meta_transaction_date` DATE NOT NULL;`
Well, that is assuming you wish to keep the column to not accept null values.
I found that the value had space before and after so I use $first = trim($first_date); and problem solved...
I am trying to get the details of a persons based on the user input age (Integer). Now my query always returns null Array() whenever I execute the below code. I haven't specified the $postdata array. You can see I have used $postdata['ageto'] and $postdata['agefrom'] are used in calculating $agefrom and $ageto
$now = new DateTime();
//Converting _POST age to Date
$agefrom = date("Y-m-d",strtotime($now->format("Y")-$postdata['ageto'].'-'.$now->format("m").'-'.$now->format("d")));
$ageto = date("Y-m-d",strtotime($now->format("Y")-$postdata['agefrom'].'-'.$now->format("m").'-'.$now->format("d")));
$this->db->select('uacc_id, uacc_username, name, dob, city, education');
$this->db->from('user_accounts as a');
$this->db->join('personal as b','a.uacc_id = b.pruserid','INNER');
$this->db->join('profession as c','a.uacc_id = c.puserid','INNER');
$this->db->join('location as d','a.uacc_id = d.luserid','INNER');
$this->db->where('dob >= ',$agefrom);
$this->db->where('dob <= ',$ageto);
$this->db->limit(10, $offset);
$query = $this->db->get();
return $query->result();
I have suspected that my input post is not fetching the data properly. So I have replaced it with my simple query select * from... where dob >.... and it worked well. So there is no problem with _POST variables. I am not sure what I am doing wrong. Can some one help me.
I think you have to specify to which table DOB belongs like a.dob. Sorry for the post but I could not comment. Hope this helps.
The select field should add the table name
$this->db->select('a.uacc_id, a.uacc_username, a.name....); // example
and your where add table name too.
$this->db->where('b.dod >= ', $agefrom); // example
or you can output the sql command.
You can look here
I need to select data from a mysql database from the past 12 months based on the current date. I have the dates saved in the database as unix timestamps but my query does not seem to work.
$query="SELECT user_id, COUNT(first_name) AS member_count
FROM main_user
WHERE renew_date<'$time' AND renew_date>'$old_time' WHERE renew_date!=''";
Basically I need to count all instances of first_name where there is a renew_date timestamp.
You have an additional WHERE where you should use AND:
$query="SELECT user_id, COUNT(first_name) AS member_count
FROM main_user
WHERE renew_date<'$time' AND renew_date>'$old_time' AND renew_date!=''";
^^^
You have an error in your query, you have two WHERE clauses!
You can find this and other errors, when you test the return value from your query
$query = 'select ...';
$result = $mysqli->query($query);
if ($result === false) {
// error handling
echo $mysqli->error;
} else {
// query successful
// process result set
}
You put WHERE twice. You can use From_UNIXTIME function in mysql
WHERE FROM_UNIXTIME(renew_date)<NOW()
AND FROM_UNIXTIME(renew_date)> (NOW()-INTERVAL 1 year)
AND renew_date !=''