mysql DATE_ADD usage in PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
modplod
Forum Commoner
Posts: 45
Joined: Mon Feb 27, 2006 11:18 am

mysql DATE_ADD usage in PHP

Post by modplod »

Hi all, I'm trying to create a report using a collection of records dated within the 7 days follow a given date. The sql statement I'm using is

Code: Select all

$query = "SELECT * FROM swnb_cards WHERE department = '$department' AND issue_date BETWEEN '$start_date' AND DATE_ADD('$start_date', INTERVAL $interval $unit )";
The above statment works fine directly on the command line, and if I use in in a single PHP page as part of a simple script Like this:

Code: Select all

$sqlOBJ = new mysql ( );
$department = 1;
$start_date = "2009-03-12";
$interval = 7;
$unit = "DAY";
$query = "SELECT * FROM swnb_cards WHERE department = '$department' AND issue_date BETWEEN '$start_date' AND DATE_ADD('$start_date', INTERVAL $interval $unit )";
$sqlOBJ->query ( $query );
But if I call it from within a class method like this

Code: Select all

    function per_department($start_date, $department, $interval, $unit) {
        // do query
        $query = "SELECT * FROM swnb_cards WHERE department = '$department' AND issue_date BETWEEN '$start_date' AND DATE_ADD('$start_date', INTERVAL $interval $unit )";
        $this->sqlOBJ->query($query);
It fails completely.

My question(s) is: Is there a better way to archive this? what is wrong in the statement that may be preventing it form returning a result when used in a class? Any ideas would be helpful I have spent way too much time on this and need a fix asap, thanks all.
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: mysql DATE_ADD usage in PHP

Post by yacahuma »

did you initialize $this->sqlOBJ ?

What error do you get?
modplod
Forum Commoner
Posts: 45
Joined: Mon Feb 27, 2006 11:18 am

Re: mysql DATE_ADD usage in PHP

Post by modplod »

yes, I set up the object,, the error does not generate a error message, but manifests its self later in the code, I tracked it to this query, which just fails to retrive any records or return any results.
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

Re: mysql DATE_ADD usage in PHP

Post by yacahuma »

did you echo the query just before executing?
modplod
Forum Commoner
Posts: 45
Joined: Mon Feb 27, 2006 11:18 am

Re: mysql DATE_ADD usage in PHP

Post by modplod »

yes I did, and as far as I can tell it looks good.
Post Reply