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

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
$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. :confused:

Any ideas please?

Cheers

Comments

  • norbie
    norbie Posts: 137 Forumite
    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";
    
  • Nope, that didn't work, thanks for trying though
  • norbie
    norbie Posts: 137 Forumite
    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";
  • Nope, hardcoding the variables does not work
  • djandyr
    djandyr Posts: 13 Forumite
    Part of the Furniture Combo Breaker
    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 Test
    CREATE 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:00
    
  • djandyr
    djandyr Posts: 13 Forumite
    Part of the Furniture Combo Breaker
    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
    
This discussion has been closed.
Meet your Ambassadors

🚀 Getting Started

Hi new member!

Our Getting Started Guide will help you get the most out of the Forum

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

Is this how you want to be seen?

We see you are using a default avatar. It takes only a few seconds to pick a picture.