extend mysql date - php

I am trying to code so that when a certain date is reached, then that is displayed in the report. What I would like to do is to find a way so that when a date is reached, ie, today, then keep the entry in the report until a user deletes it. So instead of it just showing today and not beyond, I need to find a way to show when a date is reached, it is displayed until deleted.
mysql_select_db($database_conn, $conn);
$query = "SELECT * FROM boxes WHERE customer = '$_SESSION[kt_idcode_usr]' AND destroy_date = DATE(NOW()) AND status = 1";
$result = mysql_query($query) or die(mysql_error());

SELECT * FROM boxes WHERE customer = '$_SESSION[kt_idcode_usr]' AND
destroy_date <= DATE(NOW()) AND status = 1";
this will return, when a date is reached, it is displayed until deleted.
if row is deleted no record would be there.

Related

mysql - update date field if exist

I have query that save the reservation number that user watch. the system always display the last 10 reservation per user (sort by "createDate" column), so if some reservation number is already in the last 10 the query don't do anything.
I would like to change it a little bit and if the reservation number is already in the table than to update the field "createDate" with the current date and time.
If not exist than insert new row.
What should i change?
$query = "
INSERT INTO user_last_view
(userID,
page,
refid,
mode)
SELECT ".$_SESSION['userDetails']['userID'].",
'".$page."',
".$refID.",
'".$mode."'
FROM dual
WHERE NOT EXISTS (SELECT *
FROM (SELECT ulv.userID,
ulv.page,
ulv.refid
FROM user_last_view ulv
ORDER BY ulv.createdate
DESC LIMIT 10) x
WHERE x.userID = ".$_SESSION['userDetails']['userID']."
AND x.page = '".$page."'
AND x.refid = ".$refID.");
";
$conn->query($query);

Display records in current month, using php and a mysql db

Im trying to display records in the current month from a column thats a timestamp.
This is my code
$query = "SELECT
donationid
FROM
donation
ORDER BY
donationid
WHERE
MONTH(donatedon) = MONTH(CURRENT_DATE())";
$query_run = mysqli_query($conn, $query);
$row = mysqli_num_rows($query_run);
echo '<h1>'.$row.'</h1>';
You're not checking the year, so you'll return records in the current month in any year, not just the current month.
Testing a function of a column prevents an index from being used to optimize the query. It's better to use a relational comparison if possible. You can simply create a formatted date for the beginning of the current month.
You also have ORDER BY in the wrong place, it has to be after the WHERE clause.
SELECT donationid
FROM donation
WHERE donatedon >= DATE_FORMAT(CURRENT_DATE(), '%Y-%m-01')
ORDER BY donationid

Update all rows in a selected row where time colume has time less than current time by 3 days using mysqli php

I have a table of users in my database with time column set to default timestamp and status column have value of either 0 or 1.
I already selected the rows where username='$username' and status= '1'.
Now I want to loop through these selected rows and check for rows where time in the time column is less than current time (i.e time()) by at least 7 days and then update the status column of such rows, setting status=2.
I've read a lot of things online but couldn't get to do this... Below is my current code, I'm using mysqli.
$sql= mysqli_query($conn,
"select from tables where username=$username and status='1' ") ;
$row=mysqli_query($conn, $sql) ;
I just did a work around my question above and is working perfectly ok for me now. This is what I did:
$query=mysqli_query($conn,"select * from table_name");
while($Row = mysqli_fetch_array($query)) {
$day= abs(time ()-$time)/86400;
$ql= mysqli_query($conn, "update table_name set status='2' where name='$username' and status='1' and $day>4");
//for 4 days interval
}
You don’t need to loop through it, because you can specify where condition inside your update statement:
$sql= mysqli_query($conn, "UPDATE tables SET status = '2' WHERE username = '$username' AND status = '1' AND time <= CURRENT_DATE - INTERVAL 7 DAY") ;
mysqli_query($conn, $sql) ;

How to update table if date has passed?

On my page I want to display some dates. A script needs to check if the date is in the past or in the future. If it's in the future, the date needs to be displayed. If it's in the past, I want a query to change the value of this date to "closed", so it won't be displayed any more.
What I have tried:
$deDatum = strtotime($row_originele_datum['datum']); //the date that has to be checked
$vandaag = strtotime($datum_vandaag); //today's date
$dataID = $rows_data['id']; //the message ID
if ($deDatum < $vandaag) {
$result_sluit_datum = mysqli_query($mysqli, "UPDATE belangrijkeDatum SET status = 'gesloten' WHERE id=$dataID");
}
but this seems to do nothing at all. No changes were made after running the page. What am I missing?
I think you need to change
$result_sluit_datum = mysqli_query($mysqli, "UPDATE belangrijkeDatum SET status = 'gesloten' WHERE id=$dataID");
to
$result_sluit_datum = mysqli_query($mysqli, "UPDATE belangrijkeDatum SET status = 'gesloten' WHERE id='".$dataID."'");
Essentially you are passing $dataID to your query instead of the value of $dataID.

How to only select row if table timestamp has been updated

Currently im selecting most updated row using the following php block, i have an ajax loop which runs this php block every few seconds to return feed. i want to echo false to ajax when latest timestamp hasn't changed so that ajax doesn't duplicate results and fill my Div (#feed) with identical content.
<?php
require_once 'db_conx.php';
$Result = mysql_query("SELECT * FROM profiles ORDER BY lastupdated desc limit 1") or die (mysql_error());
while($row = mysql_fetch_array($Result)){
echo $row['name'];
}
mysql_close($con);
?>
Somewhere in the session, or in the requestor, you need to store the last fetched time. It would be better to store it as a session variable (this I presume is client-specific because different clients will have loaded at different times) and then fetch all records that have their lastupdated time greater than the last_fetched time.
Everytime entries are fetched from the DB, just update the last_fetched variable to the current timestamp.
If you are running this every 5 seconds, I would do something like
$Result = mysql_query("SELECT * FROM profiles WHERE lastupdated > ADDTIME( NOW( ) , '-00:00:05.00000' ) ORDER BY lastupdated desc limit 1") or die (mysql_error());
$num_rows = mysql_num_rows($result);
if ($num_rows = 0) {
return false;
} else {
return true;
}
This will give you any rows that have been updated in the last 5 seconds, if it is older than this it should have been picked up in the last run.
Hope this helps
When you retrieve your results from the server I'm guessing you store the timestamp of the last query in some PHP variable in order to compare the returned results of the next query.
I would concatenate the timestamp into your query as a where clause so your sql query would become something like
$Result = mysql_query("SELECT * FROM profiles WHERE lastupdated > " . $timestamp . " ORDER BY lastupdated desc limit 1") or die (mysql_error());
It's also worth noting that when this executes if there has been more than one profile updated since the last update cycle then it will only return the most recently updated row.
Does this answer your question?

Categories