Database:
| date_paid (int) |
1535558400
1539532800
I am trying to query the following but i get SQL syntax error for the date part.
$id = $this->input->post('id');
$date = $this->input->post('date');
$this->db->where('id',$id);
$this->db->where('DATE_FORMAT(date_paid, "%Y-%m-%d")',$date);
$query=$this->db->get('payments');
$result=$query->row();
Is there any way to change the date format of the column date_paid so that i can actually compare it to the $this->input->post('date') data?
Error log:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''2018-10-30'' at line 4
I am trying to modify an existing web application that is why as much as possible i do not want to change the data type of the column date_paid
thanks in advance
Please, use like this
DATE_FORMAT(FROM_UNIXTIME(`date_paid`), '%Y-%m-%d') AS 'date_changed'
You can refer here
You need to use
DATE_FORMAT(FROM_UNIXTIME(date_paid), '%Y-%m-%d') AS 'date_formatted'
so something like:
$id = $this->input->post('id');
$date = $this->input->post('date');
$this->db->select("DATE_FORMAT(FROM_UNIXTIME(`date_paid`), '%Y-%m-%d') AS date_formatted");
$this->db->from('payments');
$this->db->where("DATE_FORMAT(FROM_UNIXTIME(`date_paid`), '%Y-%m-%d') = " . $date, '', false);
$result = $q->row();
Use the syntax below
select convert(varchar(4), year(ColumnDate))+'/'+convert(varchar(4), month(ColumnDate))+'/'+convert(varchar(4), day(ColumnDate)) from YourTable
Related
I want to get a time difference from data using the TIMESTAMPDIFF function, but this time i want to use a pure query builder in codeigniter
$this->db->select("TIMESTAMPDIFF(DAY, (".$this->db->select('payment_date')."), (".$this->db->select('download_date').")))",FALSE);
$query = $this->db->get('transaksi');
return $query;
I've tried the code above, but it shows an error like this :
Severity: 4096 Message: Object of class CI_DB_mysqli_driver could not
be converted to string
and like this :
You have an error in your SQL syntax; check the manual that
corresponds to your MariaDB server version for the right syntax to use
near '), ())) FROM transaksi' at line 1
SELECT payment_date, download_date, TIMESTAMPDIFF(DAY, (), ())) FROM
transaksi
is there any solution to get the data?
Solution:
$this->db->select("payment_date, download_date, TIMESTAMPDIFF(DAY, payment_date, download_date)",FALSE);
$query = $this->db->get('transaksi');
return $query->result();
Sub Query not necessary there.
$this->db->select("payment_date, download_date, TIMESTAMPDIFF(DAY, payment_date, download_date)",FALSE);
$query = $this->db->get('transaksi');
return $query->result();
Trying to get a list of expenses from a certain date, I can execute this SQL fine in PHPMyAdmin but on the page I am receiving a MYSQL Syntax Error.
The Code:
$full_date = date('d M Y', strtotime($date));
$get_expenses = mysql_query("SELECT Sum(cost) AS expense FROM expenses WHERE date = $full_date")or die(mysql_error());
$expenses = mysql_fetch_array( $get_expenses );
echo $expenses['expense'];
The syntax error I receive is:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Apr 2015' at line 1
I have checked to see the value of $expenses['expense'] and it returns a valid date. In PHPMyAdmin I can execute this SQL with no problems. Any pointers? Cheers!
try like this,before that convert your date to respective format then do like this,
$get_expenses = mysql_query("SELECT Sum(cost) AS expense FROM expenses WHERE date = '$full_date'")or die(mysql_error());
SELECT Sum(cost) AS expense FROM expenses WHERE date = 13 Apr 2015
is not a valid query, but it's what you get from that expression.
You should surround the value with quotes, such as with:
$full_date = date('Y-m-d', strtotime($date));
$get_expenses = mysql_query("SELECT Sum(cost) AS expense FROM expenses WHERE date = '$full_date'") or die(mysql_error());
I need a little assistance. I need this query
SELECT *, STR_TO_DATE( end_date, "%d/%m/%Y" ) AS DATE
FROM `resume_experience`
WHERE `resume_id` =1
ORDER BY DATE DESC
to be written in codeigniter active record format. This is what I wrote.
$result = $this->db->select('*,STR_TO_DATE(end_date,%d/%m/%Y) AS DATE')
->from('resume_experience')
->order_by('DATE', "desc")
->where($where)
->get()
->result_array();
return $result;
It is giving me the following error.
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM (resume_experience) WHERE resume_id = '1' ORDER BY DATE desc' at line 2
SELECT *, STR_TO_DATE(end_date, `%d/%m/%Y)` AS DATE FROM (`resume_experience`) WHERE `resume_id` = '1' ORDER BY `DATE` desc
Any help would highly be appreciated.
Thanks and Waiting,
Ahmad
You need to pass second parameter as FALSE in select() so it will not quote the bacticks for STR_TO_DATE(end_date,%d/%m/%Y) AS DATE
$this->db->select("*,STR_TO_DATE(end_date,'%d/%m/%Y') AS DATE",FALSE)
See active record
STR_TO_DATE() requires quotes around the format string:
$this->db->select('*,STR_TO_DATE(end_date,"%d/%m/%Y") AS DATE')
What I want to achieve is to add 30 days to a customers subscription if the payment has been made
So I am using this code
$set = "REPLACE into CLIENTS where USR_PAID = '1' and USR_EXPIRATION = DATE(DATE_ADD(NOW(), INTERVAL 0 DAY)) values ('USR_EXPIRATION','" . $due . "')";
$update = $conn->update($set, $db);
I am getting the following syntax error
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where USR_PAID = '1' and USR_EXPIRATION = DATE(DATE_ADD(NOW(), INTERVAL 0 DAY)) values ' at line 1
Statement:
REPLACE into CLIENTS where USR_PAID = '1' and USR_EXPIRATION = DATE(DATE_ADD(NOW(), INTERVAL 0 DAY)) values ('USR_EXPIRATION', '09-05-2013')
I should tell you that I am using the same function to update the db in different parts of the website and is working just fine.
Also the following query is working and gives me all the clients that have paid and expire today:
$request = "select * from CLIENTS where USR_PAID = '1' and USR_EXPIRATION = DATE(DATE_ADD(NOW(), INTERVAL 0 DAY))";
$result = $conn->query($request, G_NORMDB);
I guess it is only a syntax error that I can't figure out.
On a side note,I think this won't work anyway, you can't use replace into to selectively change columns.
Here's the manual's explanation:
Values for all columns are taken from the values specified in the
REPLACE statement. Any missing columns are set to their default
values, just as happens for INSERT. You cannot refer to values from
the current row and use them in the new row.
So you muse include the whole user row.
I think it's easier to just use this query:
$q = "UPDATE CLIENTS SET USR_EXPIRATION = '$due' WHERE `USR_PAID = '1' AND USR_EXPIRATION = DATE(DATE_ADD(NOW(), INTERVAL 0 DAY))";
$update = $conn->update($q, $db);
let me know how that works for you, if it does at all.
Your REPLACE syntax is wrong. VALUES, then WHERE.
$set = "REPLACE into CLIENTS (the two columns here) VALUES ('USR_EXPIRATION','" . $due . "') where USR_PAID = '1' and USR_EXPIRATION = DATE(DATE_ADD(NOW(), INTERVAL 0 DAY))";
Read up more on enter link description here for more information.
Edit: I was curious about not seeing WHERE in the documentation. According to this answer, REPLACE does not have a WHERE clause. Use UPDATE instead.
What about following the very advice from the error message - to check Mysql manual on the syntax for the REPLACE query?
I have this piece of code which i use to compare a timestamp field with a date i've selected before!
But i get an error in syntax..
Howcome?
$query = sprintf( 'SELECT * FROM coupon WHERE date("Y-m-d", "time") = $date' );
The error is:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' "time") = $date' at line 1
While, i'm comparing a timestamp which is the field time, and a date which is $date
Thanks in advance..
time should not be quoted as it is a field reference, not a string literal.
$query = sprintf('SELECT * FROM coupon WHERE DATE(time) = $date');
Note: You are susceptible to SQL Injection.
You have mixed up the PHP function
date ( string $format [, int $timestamp = time() ] )
and the MySQL function.
DATE( expr )
See the manual entries respectively:
http://php.net/manual/en/function.date.php
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date