The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
Calling data from table based on month, 3 month, yearly
Discuss Calling data from table based on month, 3 month, yearly in the PHP Development forum on Dev Shed. Calling data from table based on month, 3 month, yearly PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 16th, 2013, 05:51 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 22
Time spent in forums: 3 h 52 m 53 sec
Reputation Power: 0
|
|
Calling data from table based on month, 3 month, yearly
Looking to be able to recall data from a table based on the current month, past 3 months, and past year for the $_SESSION of the current user. Here's what I have so far but it doesn't work:
PHP Code:
$result = mysql_query("SELECT * FROM invoices WHERE username ='".$_SESSION['username']."'AND invoicedate = DATE_SUB(CURDATE(), INTERVAL 30 DAY)")
I have also tried:
PHP Code:
$result = mysql_query("SELECT * FROM invoices WHERE username ='".$_SESSION['username']."'AND MONTH(invoicedate) = MONTH(CURRENT_DATE)")
I know that the username in the session is being stored and the username is also in the table based upon the current user. The second PHP code actually recalls data, but it recalls the month of June and only one line of the invoice from the month of June, not all 4. The table has the date format of 1/2/2013. Do I need to change the date format?
|

January 16th, 2013, 06:06 PM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
Hi,
Quote: | Originally Posted by derektoews The table has the date format of 1/2/2013. |
That's not possible -- unless your "dates" are in fact strings. This would be a very bad thing, because you'd circumvent the type system of the database. Strings can hold anything, so you have absolutely no guarantee that your "dates" are actually sensible. They could be complete gibberish like "*"$#1". And your date strings are in fact gibberish, at least to MySQL, which only knows the US format.
So don't misuse strings for dates. Use the actual date types provided by MySQL.
Another big problem is that your queries are vulnerable to SQL injections, and the "mysql_" functions are obsolete. Switch to a modern database extension and use prepared statements.
|

January 16th, 2013, 06:07 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
|
#1. Are you sure you want it to retrieve invoices that are from exactly 30 days ago? Or ones on or after 30 days ago?
#2. I'm quite sure that query is not retrieving anything from June since the CURRENT_DATE is in January.
Dump out the data in the invoices table and check that it has what you expect it to have.
|

January 17th, 2013, 02:05 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 22
Time spent in forums: 3 h 52 m 53 sec
Reputation Power: 0
|
|
Figured out that I just needed to change the date format (thanks to some friendly help) and now I can pull data based on the current month. What I just realized, though, is I'm trying to pull the data from the previous month (so currently I'd be trying to pull data from December, 2012).
So this works great for pulling data from the current month, but how do you pull data from a previous month?
PHP Code:
SELECT invoicenumber FROM invoices WHERE username ='".$_SESSION['username']."'AND MONTH(invoicedate) = MONTH(CURRENT_DATE)
|

January 17th, 2013, 02:31 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
Well, using
Code:
CURRENT_DATE - INTERVAL 1 MONTH
is certainly an option.
|

January 17th, 2013, 03:02 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 22
Time spent in forums: 3 h 52 m 53 sec
Reputation Power: 0
|
|
|
that works great!
So then if I understand it correctly, if I say INTERVAL - 8 MONTH then it will select everything from now till 8 months ago? or just what is located at the 8th MONTH?
|

January 17th, 2013, 03:26 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
|
Whether it's "from now till" or not depends on the query. With the latest one you've posted it'll only find stuff that happened in that month (of any year).
|

January 17th, 2013, 04:24 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 22
Time spent in forums: 3 h 52 m 53 sec
Reputation Power: 0
|
|
|
..
|

January 17th, 2013, 04:31 PM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 22
Time spent in forums: 3 h 52 m 53 sec
Reputation Power: 0
|
|
Ok, so how would I specify a specific year, and also, the query is setup to select all within the past 8 months, but it's not selecting the current month, which is fine for what I'm currently doing, but how would I select just a specific month, say, October of 2011.
PHP Code:
SELECT itemprice FROM invoices WHERE username ='".$_SESSION['username']."'AND MONTH(invoicedate) = MONTH(CURRENT_DATE - INTERVAL 1 MONTH)
|

January 17th, 2013, 05:32 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
"The past 8 months" is the same as "anytime after 8 months ago".
Code:
invoicedate >= CURRENT_DATE - INTERVAL 8 MONTH
For a specific month of a year you can specify the year as well
Code:
MONTH(invoicedate) = 10 AND YEAR(invoicedate) = 2011
or the clever solution of
Code:
invoicedate BETWEEN "2011-10-01" AND "2011-10-99"
(which I'm pretty sure you can get away with).
Comparing a DATE_FORMAT() to a string is another option.
|

January 18th, 2013, 08:27 AM
|
|
Registered User
|
|
Join Date: Dec 2012
Posts: 22
Time spent in forums: 3 h 52 m 53 sec
Reputation Power: 0
|
|
|
gotcha. Makes sense! I've got everything I need figured out for now, thank you very much!
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|