How to update SQL date using NOW() [duplicate] - php

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 7 years ago.
Basically I have a simple login form. In the database I have a 'last_logged' column and would like to update it with the current date and time every time someone logs in.
I currently have this query:
UPDATE users SET last_logged = "NOW()" WHERE id = 1
But it doesn't update the column to the current date. Any ideas why?

Remove the quotes from NOW(). As a function call, it should be unquoted.
UPDATE users SET last_logged = NOW() WHERE id = 1

MS SQL uses GETDATE() rather than NOW()
(Just an FYI)
In SQL-Server I now use SYSDATETIME():
DECLARE #now DATETIME = DATEADD(dd,DATEDIFF(dd,'19000101',SYSDATETIME()),'19000101');

Related

I want to check if column value is today date in mysql [duplicate]

This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 2 years ago.
I am using this code.
In column there are values of 2020-07-06 11:59:34 and 2020-07-06 11:59:45.
I don't want to compare time but only date. I want to check if column value is today date.
But the output of this code is 0.
require_once "connect.php";
$followers = $conn->query("SELECT * FROM followers WHERE DATE('created_date') = CURDATE() and profile_id=1");
echo $followers->num_rows;
don't use single quotes for columns name (eventually use backtics when needed)
SELECT * FROM followers WHERE DATE(created_date) = CURDATE() and profile_id=1

SQL Select: Multiple values [duplicate]

This question already has answers here:
Datetime equal or greater than today in MySQL
(10 answers)
MySQL Where DateTime is greater than today
(3 answers)
Selecting entries by date - >= NOW(), MySQL
(6 answers)
Select record(s) from mysql table where date is greater than or equal to today
(3 answers)
MySQL query select all were date is equal to today on datetime
(2 answers)
Closed 3 years ago.
Would somebody mind helping me with this?
I'm trying to select all bookings from my table with a start date of today or in the future. The issue i'm having is that I already have a limit (pagination) and sort.
$today = date("Y-m-d");
$sql = "
SELECT *
FROM `bookings`
WHERE startdate >= $today
ORDER BY `startdate` ASC
LIMIT $offset, $no_of_records_per_page";
Edit: The code above displays all bookings regardless of date, rather than just present/future bookings.
Look at the generated SQL statement -- the current date in the SQL query should be something like '2020-01-02' (including the single quotes). Without the quotes, you'd get weird behavior like the database doing the subtraction and comparing the date against 2020-01-02 = 2017!.
WHERE startdate >= $today
Your immediate problem is that you are not surrounding the date variable with single quotes, so you end up with something like where start_date >= 2020-01-13. MySQL sees an arithmetic operation (2020 minus 1 minus 13 = 2006) and happily executes it. Now it needs to compare column startdate (which is of date datatype or the like) to an integer: for this, it implicitely casts startdate to unsigned: this generates a number like 20200113, which is much bigger than 2006. This is not what you intend.
This woud not happen if you were using parameterized queries.
But bottom line, why bother computing the current date from PHP when MySQL has a built-in for that? Just do: WHERE startdate >= CURRENT_DATE and you are all set.

Date not getting entered in mysql(0000-00-00) using php [duplicate]

This question already has answers here:
Php/Mysql date saved as '0000-00-00'
(5 answers)
Closed 6 years ago.
$dat=date("Y-m-d");
This is how date is being fetched in PHP
insert into gd_purchase(Product_Model,Purchase_Date,Purchase_Quantity) values('22P413',2016-12-27,20)
When above query is fired date is entered as 0000-00-00 in mysql
You need to wrap the date value with single quotes like this:
insert into gd_purchase(Product_Model,Purchase_Date,Purchase_Quantity)
values('22P413','2016-12-27',20)
you need to add date part in quotes:
insert into gd_purchase(Product_Model,Purchase_Date,Purchase_Quantity) values('22P413','2016-12-27',20)

Increase all date_time or date columns by a certain interval [duplicate]

This question already has answers here:
Update date + one year in mysql
(3 answers)
Closed 6 years ago.
Is there any way to increase all date_time or date columns in all the tables in a database by a certain interval (for e.g. 1 year)?
Is there any php script or SQL query to achieve the same?
FOR SQL SERVER, YOU CAN DO THIS:
select dateadd(year,1,your_date_column)
from table;
FOR NETEZZA, YOU CAN DO THIS:
select add_months(your_date_column,12)
from table;
I guess MySQL should have the samiliar function.
I am not quite familiar with PHP though.
But generally speaking, I think that's a very simple function for sql tools.

How do I make now() be yesterday [duplicate]

This question already has answers here:
MySQL selecting yesterday's date
(8 answers)
Closed 9 years ago.
I want to set dealend to be Yesterday instead of now. Is there a way to do it in this command line?
$query = mysqli_query($myConnection, "UPDATE bookdeals SET dealend='now()' WHERE id='$pid'") or die (mysqli_error($myConnection));
... SET dealend = now() - INTERVAL 1 DAY
Note that with the 'now()' you're using, you're not using a function called "now". You're trying to set your table field to be a string whose contents are the letters n, o, w etc.... Quotes turn things into strings and those things lose their specialty once they've been stringed.
now() is a SQL function that is determined by the starting time of the transaction. You can changed the value of it by calling
SET TIMESTAMP = ...
However, for this to work it would need to be in the same transaction as your UPDATE query. What I would recommend instead is doing something like:
UPDATE bookdeals SET dealend=DATE_SUB(NOW(), INTERVAL 1 DAYS) WHERE id='$pid'
This uses the MySQL SUB_DATE() function, which will subtract one day from the current time.
You could use the MySQL DATE_SUB function
... dealend=DATE_SUB(now(),INTERVAL 1 DAY) ...
Read here for more information about MySQL date function

Categories