SQL query in PHP script

Posted on 2011-05-09
Last Modified: 2012-06-27
Im trying to get the information from the database for the last 30 days .
when I'm performing the search directly in MySQL (phpmyadmin) its does retrieve results but in the php script from some reason its doesn't seem to return something

$query = "Select post_content from wp_posts WHERE post_date >= ADDDATE(curdate(), interval -30 day)";
Question by:Nura111
    LVL 107

    Expert Comment

    by:Ray Paseur
    Maybe you can use PHP to generate the date.

    $last_30 = date('c', strtotime('Today - 30 days'));
    $query = "Select post_content from wp_posts WHERE post_date >= '$last_30' ";
    LVL 40

    Expert Comment

    Can you show your code? The line you gave is just building of the SQL, but not the execution of it.
    LVL 33

    Expert Comment

    The SQL looks fine, including the functions.

    I think we might need to see the rest of the code.

    Perhaps results are being returned but not in the way you expect, so the rest of the code isn't processing them properly
    LVL 4

    Expert Comment

    You may try this:
    $query = "SELECT post_content FROM wp_posts WHERE post_date BETWEEN SYSDATE() - INTERVAL 30 DAY AND SYSDATE();"
    $query = "SELECT post_content FROM wp_posts WHERE post_date BETWEEN DATE_SUB(NOW(), INTERVAL 30 DAY) AND NOW();"
    $query = "SELECT post_content FROM wp_posts WHERE post_date BETWEEN DATE_SUB(CURDATE(), INTERVAL 30 DAY) AND CURDATE();"


    LVL 40

    Accepted Solution

    Sure he can try all of those queries but his SQL is already valid, so until we see the rest of the code we won't see the problem.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
    PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to dynamically set the form action using jQuery.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now