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
Related
I sent parameter date to check if that date between two date from database.
example date string parameter that i sent to API is "09/01/2020" which format is mm/dd/YYYY
this format also same for date string in database table column.
Now in database, I put date_start as "08/21/2020" and date_end as "09/05/2020"
My code is :
$date = $data["date"];
to get date string parameter that I sent to API.
Example table name is promo,
my code is :
$sql = "SELECT * FROM promo WHERE STR_TO_DATE('" . $date . "', '%M%M/%d%d/%Y%Y%Y%Y')
between date_start and date_end";
But I did not get it.
How to correct that to get it?
Edited my latest code is like this but still did not get it:
$date = $data["date"];
$date = DateTime::createFromFormat('m/d/Y', $date);
$date = $date->format('m/d/Y');
$sql = "SELECT * FROM promo WHERE $date BETWEEN STR_TO_DATE(date_start, '%m/%d/%Y') AND
STR_TO_DATE(date_end, '%m/%d/%Y')";
You said:
Now in database, I put date_start as "08/21/2020" and date_end as "09/05/2020"
This is not best practice, and you should always store your dates as a proper date or datetime type in MySQL. Had you done this, you would only need a range comparison in your query:
SELECT *
FROM promo
WHERE ? BETWEEN date_start and date_end;
If you must stick with your current design, then you would actually have to use STR_TO_DATE on the start and end dates:
SELECT *
FROM promo
WHERE ? BETWEEN STR_TO_DATE(date_start, '%m/%d/%Y') AND
STR_TO_DATE(date_end, '%m/%d/%Y');
To the ? placeholder above, you should bind either a valid PHP date, or a valid MySQL date literal string. But again, I strongly recommend fixing the design problem.
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
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 have a mysql datetime field that stores dates in the form '2013-12-25 00:00:00'
I need to select all records for any month in the table with a query like:
$sql = "SELECT *
FROM `images`
WHERE (photodate BETWEEN '2003-11-01 00:00:00' AND '2003-12-03 00:00:00')
ORDER BY photodate DESC
LIMIT 30";
The above select query does the job fine.
In order to change the dates, I need to replace the '2003-11-01 00:00:00'AND'2003-12-03 00:00:00' with variables, so I set a variable with input data from two drop down lists for $startyear and $startmonth and convert it to what I think is the correct form using:
$startdate = $startyear."-".$startmonth."-01 00:00:00";
I do the same to the $enddate by adding 1 to the $startmonth.
My code then becomes:
$sql = "SELECT *
FROM `images`
WHERE (photodate BETWEEN $startdate AND $enddate)
ORDER BY photodate DESC
LIMIT 30";
This does not work at all and gives a MySQL error. Having struggled with it for a month and finding nothing on any forum that uses variables instead of text, I am totally at a loss as to how it could be done. All help appreciated.
You are vulnerable to SQL injection attacks, which is why it's not working. You're producing the literal query
... WHERE (photodate BETWEEN 2003-11-01 00:00:00 AND 2013-12-03 00:00:00)
The 2003-11-01 and 2013-12-03 will be interpreted as a series of mathematical subtractions, and the 00:00:00 will be a simple flat-out syntax error. You need to, at bare minimum, quote those values:
... WHERE (photodate BETWEEN '$startdate' AND '$enddate')
^----------^-----^--------^--- note the quotes
so that mysql can see the WHOLE date as a date value, and not some arbitrary broken strings.
I guess you're missing some apostrophes... try this:
$sql = "SELECT * FROM images WHERE (photodate BETWEEN '$startdate' AND '$enddate') ORDER BY photodate DESC LIMIT 30";
You could have problems with the logic. In $enddate doesn't adding 1 to the start month give you 13?
Try printing out the contents $sql when the variables are in and see how it compares to the working $sql.
Please add apostrophes your query (and sanitize your variables using mysql_real_escape_string, PDO bind values, mysqli_real_escape_string) :
$sql = 'SELECT * FROM 'images' WHERE (photodate BETWEEN '.$startdate.' AND '.$enddate.') ORDER BY photodate DESC LIMIT 30';
A little reminder, you shall NOT use MySQL (deprecated, old.. and not that fast), if you're using MySQLi or going to use it, please sanitize your variables like this, as Marc B said it could break your script and your app security :
<?php
// Starting MySQLi Connection
$db = mysqli_connect("host", "user", "password", "dbname");
// Sanitizing your variables
$startdate = mysqli_real_escape_string($db, $startdate);
$enddate = mysqli_real_escape_string($db, $enddate);
// Query
$sql = "SELECT * FROM 'images' WHERE (photodate BETWEEN ".$startdate." AND ".$enddate.") ORDER BY photodate DESC LIMIT 30";
// Doing the query and print the result array
$var = mysqli_query($db, $sql);
print_r($var);
// Closing connection
mysqli_close($db);
?>
Please refer to to this for PDO way or to this for MySQLi way, you can also check the MySQL_real_escape_string into PHP doc but MySQL functions are deprecated since PHP 5.5
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?