Possible to get data added date in phpmyadmin using php mysql? - php

I created database but not added date column, data added last 1year but not show data added current date, possible to get data added current date, please help me
my database name studen
name
age
city
but not added date column, possible to get student joining date

No, it's not possible. If you need to know when a row was added, you should have a column like
date_added TIMESTAMP DEFAULT CURRENT_TIMESTAMP
and if you want to know when the row was modified, you can use a column like
last_modified TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
If you don't have anything like this in the table, MySQL doesn't save the dates automatically.

Related

how to keep the date unchanged when changing other data?

I want to update the status column in my table, but the date column of the message is also updated. How can I only update the status column only without the date field ?
Data type:
Status => Varchar
messgae_date => Timestamp
Query :
"UPDATE mytable SET status='1' WHERE status='0'"
Thank's before ..
Please edit your question and post the entire DDL statement for creating the table. I believe it will become apparent.
Your post said that message_date is a timestamp. That's a special datatype in MySql and MariaDb that can autoupdate itself at insert or whenever any other data in a row is changed.
An auto-updated column is automatically updated to the current
timestamp when the value of any other column in the row is changed
from its current value. An auto-updated column remains unchanged if
all other columns are set to their current values. To prevent an
auto-updated column from updating when other columns change,
explicitly set it to its current value. To update an auto-updated
column even when other columns do not change, explicitly set it to the
value it should have (for example, set it to CURRENT_TIMESTAMP).
Again, please post the table DDL statement. The message_date column will probably look something like this.
`message_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP

How to automatically store current system time on record generation in MySQL?

I'm creating a database for an application. The table in question will be a log of all purchases made through the app, and one of the columns in the table will store the current date & time in DATETIME() (YYYY-MM-DD HH-MM-SS format).
I know the SQL function Now() returns the current system date and time in this format, but can I create the table so that each new purchase automatically stores the current system time? I'm trying to do something like this:
CREATE TABLE purchase_log (
//bunch of code
timestamp DATETIME() NOT NULL DEFAULT Now(),
//more code
);
So that by default each entry into purchase_log will automatically store the value returned by Now(), but I don't think this is valid SQL syntax. What would be the best way to do this?
Thanks!
CREATE TABLE purchase_log (
`addedwhen` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
)
I used the fieldname "addedwhen" as "timestamp" is a MySQL reserved word.
Ref: https://dev.mysql.com/doc/refman/5.5/en/keywords.html
but I don't think this is valid SQL syntax.
How? It's absolutely valid; Did you actually tried running that? If not then see here http://sqlfiddle.com/#!9/e0b1a7/1
CREATE TABLE purchase_log (order_id int not null,
order_name varchar(10),
orderdate DATETIME NOT NULL DEFAULT Now()
);
insert into purchase_log(order_id, order_name) values(1,'laptop');
Add a default value to the field as CURRENT_TIMESTAMP.
However, while inserting data into the table, you will need to be careful.Either pass NULL to the field, or ensure proper timestamp.

Setting DATETIME to automatically go into database table

I have set my database field to DATETIME, and when a user submits the form I want to automatically insert the current date/timestamp. How do I do this?
So far I have..
INSERT INTO expenses(user_id, amount, currency, type, description, project_id, filename)
VALUES("30","55","2","6","jult","30",DATETIME,"P")
Use NOW() instead of DATETIME on your query.
Sidenote:
Also, you will face an error on query as you are passing just 7 columns, but you are inserting 8 values.
First, you need to create a column in your database to store your datetime.
Second, rather than using "DATETIME", using gmdate("Y-m-d H:i:s") to generate a GMT/UTC datetime or date() to a local time
There's no field name in your query that I can assume is a DATETIME field, but you can use NOW(), CURRENT_TIMESTAMP, or CURRENT_TIMESTAMP().
Of course this is assuming you're using MySQL.
The easiest way would be to set a default on the row, if you can:
CREATE TABLE expenses (
/* Other row definitions */
date DATETIME DEFAULT (strftime('%s', NOW()))
);
That way you can omit the row in your insertion statement entirely.
You could use CURRENT_TIMESTAMP where you have DATETIME.
In MySQL, you cannot use a volatile function such as now() as the default for most columns. The one exception is for a timestamp column. So the create table statement could look like:
create table expenses (
. . .,
CreatedAt timestamp default current_timestamp
);

storing the updated timestamp whenevr the query from that particular table is called

I want to store the time at which the table was accessed/modified. I know how to put current timestamp.
But what we must do to store the updated timestamp ?
I am not sure what exactly you are looking for but below query might be helpful.
you can create/upate your table to have "On Update Current_timestamp"
CREATE TABLE t1 (
ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
To update a timestamp when accessing a record you could use a stored procedure to select the records and then update the timestamp fields.
#Suresh Kamrushi has suggested a solution for updating the timestamp when the record is modified.

php mysql timestamp works only on update

i am using a timestamp type column in my mysql database, and i have enabled the option for auto update timestamp, but the time stamp works only on update command and not on insert
i am using navicat mysql client
Thanks in advance.
Try assigning the current timestamp as the default value along with enabling auto-update. That should work for you.
See: current_timestamp
Did you mean something like this..
To set the TIMESTAMP column in either table to the current timestamp at insert time, explicitly assign it that value.
INSERT INTO t1 VALUES (NOW());
INSERT INTO t2 VALUES (CURRENT_TIMESTAMP);
http://dev.mysql.com/doc/refman/5.0/en/timestamp-initialization.html
Set the column default value to CURRENT_TIMESTAMP.
If you are using a server side language to insert data into the table, you could simply insert the current date into that column. In PHP, for example, that would be
<?php
$time = time();
?>

Categories