We’d like to remind Forumites to please avoid political debate on the Forum.
This is to keep it a safe and useful space for MoneySaving discussions. Threads that are – or become – political in nature may be removed in line with the Forum’s rules. Thank you for your understanding.
📨 Have you signed up to the Forum's new Email Digest yet? Get a selection of trending threads sent straight to your inbox daily, weekly or monthly!
The Forum now has a brand new text editor, adding a bunch of handy features to use when creating posts. Read more in our how-to guide
PHP/SQL Help
FreheitOverture
Posts: 518 Forumite
in Techie Stuff
Ok, so I'm completely confuzzled with this. I'm passing a variable into an SQL query and trying to retrieve all records in that table where the month equals the value of the variable. i.e. if I pass the variable 10 into the query, it should show all records in October.
So far I have this

Any ideas please?
Cheers
So far I have this
$query="SELECT * from news WHERE EXTRACT(MONTH FROM TIMESTAMP 'date') = $month ORDER BY date DESC LIMIT $start, 4";however, while I am not getting a syntax error, I should have 3 records displayed and have none.
Any ideas please?
Cheers
0
Comments
-
Try it with quote marks used around the variable e.g.
$query="SELECT * from news WHERE EXTRACT(MONTH FROM TIMESTAMP 'date') = [B]" .[/B] $month [B]. "[/B] ORDER BY date DESC LIMIT [B]" .[/B] $start [B]. "[/B], 4";
0 -
Nope, that didn't work, thanks for trying though0
-
If you hardcode both variables to something, does it work?
e.g.
$query="SELECT * from news WHERE EXTRACT(MONTH FROM TIMESTAMP 'date') = 10 ORDER BY date DESC LIMIT 0, 4";
It looks like you will need some quote marks around that 10 as well, so try it like this:
$query="SELECT * from news WHERE EXTRACT(MONTH FROM TIMESTAMP 'date') = "10" ORDER BY date DESC LIMIT 0, 4";
If that works, then change your code to:
$query="SELECT * from news WHERE EXTRACT(MONTH FROM TIMESTAMP 'date') = "" . $month . "" ORDER BY date DESC LIMIT " . $start . ", 4";0 -
Nope, hardcoding the variables does not work0
-
The field "date" is a reserved word within MySQL, this can be permitted but you will need to quote those identifiers with a backtick (`)
[php]$query="SELECT * FROM news WHERE
EXTRACT(MONTH FROM TIMESTAMP (`date`)) = $month
ORDER BY `date` DESC LIMIT $start, 4";[/php]Its good practice to escape any variables you pass into a query, this is for security to prevent any SQL injections. You can use a function available with PHP called mysql_real_escape_string.
Query TestCREATE TEMPORARY TABLE news_test (date DATETIME); INSERT INTO news_test VALUES ('2008-08-01 09:00:00'); INSERT INTO news_test VALUES ('2008-09-01 10:00:00'); INSERT INTO news_test VALUES ('2008-10-01 11:00:00'); INSERT INTO news_test VALUES ('2008-10-02 13:00:00'); INSERT INTO news_test VALUES ('2008-10-03 17:00:00'); SELECT * FROM news_test WHERE EXTRACT(MONTH FROM TIMESTAMP (`date`)) = 10 ORDER BY `date` DESC LIMIT 0, 4; Result : 2008-10-03 17:00:00 2008-10-02 13:00:00 2008-10-01 11:00:000 -
Just thinking you could also use the MONTH() function with MySQL, probably reduce the complexity of the query
SELECT * FROM news WHERE MONTH(`date`) = 10 ORDER BY `date` DESC LIMIT 0, 4
0
This discussion has been closed.
Confirm your email address to Create Threads and Reply
Categories
- All Categories
- 353.6K Banking & Borrowing
- 254.2K Reduce Debt & Boost Income
- 455.1K Spending & Discounts
- 246.6K Work, Benefits & Business
- 603K Mortgages, Homes & Bills
- 178.1K Life & Family
- 260.7K Travel & Transport
- 1.5M Hobbies & Leisure
- 16K Discuss & Feedback
- 37.7K Read-Only Boards