I am making an auction website, where users can add products, and set the end date.
My table enddate is like: 2015-2-25 14:01:23
and to determine if the item is ended I have a row called ended which can be 0 or 1. While I am writing this, I am thinking maybe I should check if time(system) is after or before enddate row to determine if the item is ended...
Anyway How would I go about changing the row ended to 1 on the exact time and date (even if someone's not viewing the page to run an ajax script) Would I have to do chron, or would it be better if I done what I mentioned above regarding checking the times?
Which one would you recommend? If using row ended how would I accomplish updating this on the enddate time.
Well, that's not the way to use a database. Your own suggestion is the way to go: Check if the current time is after or before enddate row to determine if the item has ended...
Related
I need some help about database management. I am trying to retrieve data from my database (filtering them by the created_at field).
There will be no problem when I am retrieving data from my database created in today's date.
For example today is 4/17. When I run the insert function today, the value for created_at will be 4/17 as well. So when I go to my web page and display data for 4/17, the data will be right.
But let's say I forgot to fetch data for 4/15, and I need to fetch those data today. When I insert these data in my database now, the created_at will be 4/17, but the adjacent data is actually for 4/15.
Now, when I go to my web page and display data for 4/15, I will get nothing.
As a workaround, I added a date field in my table, and this will contain a specified date, unlike the created_field that takes the server's date. I now use the date field to filter the data in my web page.
However, I think this is somewhat redundant or inefficient approach. Does anyone have any suggestions?
Here is a screenshot of my current table structure:
The accepted answer solves the XY problem. It's probably not the way to solve the actual problem.
There are lots of reasons for putting the current datetime into a database (rather than a datetime which is intrinsic in the data such as an appointment, or a date of birth). While this shouldn't be used for auditing purpose is it is handy for debugging and for dealing with optimistic locking.
But here, you seem to be looking for a transaction control mechanism - a way of identifying what records have been subjected to some action. If it is important to maintain a record of the sequence in which the records were processed, then a date, or even a date time, or even a millisecond timestamp may not be adequate. What happens iwhen you need to apply this operation more than once per day? What if it fails half way through and you need to resume the operation? This mechanism also precludes the notion that there may be more than 2 stati for a record.
Assuming the thing which is being done with the record is part of an ACID transaction, then there are 2 states to consider - before and after. Your data domain should explicitly describe those two states (using a null/non-null date value is merely implicit). If the transaction is not atomic then there will likely be more states to consider.
In the case of MySQL I would implement this as an enum datatype (with a non-null constraint). But more generally I seek to avoid a situation where data is being updated like this by using synchronous operations wrapped in transactions.
Since you are using Laravel, you can simply override the created_at value when creating your model. So for example, you can do the following:
$myModel->created_at = Carbon::parse('2019-04-15');
$myModel->save();
This would set the created_at value to April 15th, not today. Hence you don't need a second date column in your table.
UPDATE
Nonetheless, if you need the time part to still reflect the current time, you can do the following:
$myModel->created_at = Carbon::now()->setYear(2019)->setMonth(4)->setDay(15);
$myModel->save();
Hi I have a MySQL table of Facebook pages (fbpagesfancount) that has the total fan count by day since 01 Jan 2016.
The structure is like this:-
Pageid, Pagename, Updated_Date, Fan_Count
There are a number of specific days that are missing and do not therefore have fancount values due to Facebook API issues.
The days that are missing are usually single days, for example, there is a value for the day before and the day after.
I'd like to create a new table that has a record for every day since 01/01/2016 for each page (750 pages) and then update the days that are missing by averaging the day before and the day after the missing date.
Is this possible using MySQL only or should I write a script in PHP that performs this task and if so, any suggestions on the logic would be helpful.
Any other suggestions on how to tackle this issue would be welcome.
Thanks
Jonathan
Yes, it is possible in SQL only.
No, you should not attempt it as it is more complicated and for a single shot there's no need.
Yes, write a script in any language you know, for instance PHP.
I'm not sure why you even want to create a new table? You could add a flag to your current table saying its an origional count vs an average, and just find the missing numbers and add them in a script.
I have a dynamically changing mysql table(issuelist) with columns issueno.,issuetime and status. I display only issues with certain status(bad) in a html page.
"select issueno.,status from issuelist where status='bad' group by issueno.;"
Now I want to make the webpage static from Sunday to next Sunday, i.e., if the status of an issue changes from bad to good on tuesday, even then I want the page to display that particular issue with status = bad till next Sunday.
How can I achieve this?
You need to store the data somewhere in your database (for example you could add a column fixdate that would be NULL for issues with status that was good from the beggining or the date of the fix for the issues that changed status. That way, you could specify a static date to compare with the fixdate then show 'bad' or 'good' depending on the case.
Also you don't need to GROUP BY in a single table query with no operation
I am developing ecommerce store in php and I have some problem in creating a logic. The problem is I have a store page where I am showing some products. all the products have some time interval,after interval passes the products will no longer be display there.
For example
Product: jeans
time left: 10 days.
after 10 days jeans product will no longer be there. in database I have a set a field with the name active_status which accepts Y or N..
I know that I can simply run the update query and set the status to "N" after time passes. here in this example after 10 days
BUT the question is WHEN DO I RUN THIS UPDATE QUERY ?
should I always check time and run again and again update query and set STATUS TO 'N'??? IS that is the only solution ?
I mean usually we do like for example if customer logins we set some status or any other event but here we are setting the status against checking the time. Hopefully you have understand my question
In the db I am saving the start time and number of days which user puts through the admin panel
My first shot would be cron, php script and properties table (if needed, because for simple uses you could just store expiration date inside business entity).
Cron runs php script periodically (e.g. once a day),
scripts checks if there is anything to delete, based on properties table, or entity properties.
If there is anything to delete, script performs deletion of selected content.
That's all and it is in fact very popular scenario.
More on cron: http://www.pantz.org/software/cron/croninfo.html
Here is my logic i hope it will helps i think
While Publishing the product we have to maintain the time interval of that product for example if you want to display the product for 10 days give 10 days and date of publish product.
By comparing with that date and number of days given for time interval with the present date i.e today's date
Can you check with this
I've been tinkering with PHP lately (self-taught, no formal training), trying to understand how to grab data from a database and display the data somewhere. So far I have learned quite a bit, but now I am stumped.
I have a list of about 200 users in my local database in a table called site_members. The table has three fields: id, name, birth_date. Via PHP, I want to display all the users on a webpage, and have something like "Birthday soon!" be mentioned right after their name. Something like this:
John Smith (Birthday soon!)
I haven't written the code to do this, because I usually write pseudocode first before actually diving into the coding part. Here's the pseudocode:
Get the current date and time and convert it to Unix timestamp
Start foreach loop and go through list of users
Query the database table, get the birthdate of a user by their id, and store it in a variable named bdate.
Convert bdate to Unix timestamp
Subtract the current date from bdate, convert it into days remaining, and store it in a variable called remaining_days.
If the user's bdate is within 15 days (remaining_days is less than 15)
Display their name, followed by (Birthday soon!)
otherwise
Just display their name only
End if
End foreach loop
Here's my problem: With the above pseudocode once translated into actual code, there would be a database query made every time in that foreach loop. Some of the tutorials I consulted mentioned I should avoid that for efficiency reasons, and it makes sense. I ran Google searches to find something similar, but that didn't do much. I do not want anyone to write any actual code for me. I just want a better solution to the querying.
Thanks in advance!
I think your concept for the pseudo code is right, and you're understanding of doing multiple database queries is also right, you just tangled the two into giving you a wrong idea.
If you construct your select statement properly (that's basically what you'd be using to access the database), you actually pull the information for everyone out of the database and store it once in an array (or some other form of object). You can then start your foreach loop using the array as your value and perform the rest of your checks that way.
$date = date("m.d.y");
$people = ** insert your commands to grab the info from the DB **
foreach($people as $person) {
// do your comparison checks and echo's etc in here
}
Does this make sense?
There can be two solutions to your problem:-
1:
Instead of making query for every user, first get the data for all the users.
Traverse the data using foreach loop php
Do the processing and display the results.
2:
Store the user date_of_birth in proper mysql date datatype
Change your mysql query to use date function to get all the users who match your date difference criteria and just display those users.
It seems you failed to read up properly on the relationship between SQL and PHP. If you actually posted code, then you could have been easily unstumped because there are many ways to do the simple task from legacy tutorials to current PDO or even MVC within in 5mins or less.
I'm not going to write the code but you need to change HOW you think in your "pseudo code".
The problem with your pseudo code is because you believe that the DB is not smart and you are doing it as if it was only for storage.
The correct pattern for PHP is the following:
1) use the Date function to retrieve current day + 15. Get month and
day only.
2) you make a SQL query that retrieve all users who's
birth_date field's month and day are GREATER THAN (or equal) to
TODAY and who are less than or equal to today + 15 (day and month
only)
3) execute the query.
4) with the returned data set (if any)
you can choose two path depending situation and design
a) you can loop it with a simple FETCH which fetch each row retrieve
and display name and extra message.
or
b) iterates through the result set and store the display message
into a variable and then finally display it once the iteration is
done.
(option b is prefered because its more flexible since you can use this technique to out into a file instead of an echo)
THIS pseudo-code ensures that you are only retrieve the correct data set with the aid of the SQL system (or storage system).
In terms of overall process, aashnisshah is absolutely correct. First, you should retrieve all the records you need from your database then loop through each row to do your data comparisons and finally close the loop.
As for finding out if their birthday is close and if you want MySQL to do the hard work, you can build your query like that in PHP:
$query = "SELECT *, DATEDIFF(DATE_FORMAT(dob, '" . date('Y') . "-%m-%d'), CURDATE()) AS days_to_dob FROM Members";
The idea is to fetch an extra column called 'days_to_dob' containing the amount of days until that person's date of birth. Note that it will be negative if that date has passed for this year. With that extra column you can easily evaluate whether their dob is within 15 days.
If you don't want any php code, then here is my pseudocode:
Get date and time -> UTC stamp and store in $time_current
Get all from site_members and store in $data
for each entry in $data, store in $record
get birth_date from $record and convert to utc stamp and store in $birthday
print name from $record
if $birthday is close to $time_current then print "Birthday soon" end if
print new line
end for
That performs only one request to your database.