Jump to content

Recommended Posts

This is what I have at the moment but nothing shows up.

 

<?php $get_lastweek = mysql_query("SELECT SUM(total) FROM invoices WHERE created >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY AND created < curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY");

$show_lastweek = mysql_fetch_array("$get_lastweek"); ?>

<?php echo $show_lastweek; ?>

It may not be the problem, PHP is pretty forgiving, but why do you have $get_lastweek inside double quotes?  I mean, double quotes WILL parse the variable, but I've never ever seen anyone pass a resource value that way.  Try it without the quotes, just so I don't go crazy...

 

Also, the use of short tags .... ?> suggests to me that you are using an old version of PHP

Try this

<?php
$get_lastweek = mysql_query("SELECT SUM(total) FROM invoices WHERE date_created BETWEEN DATEADD(wk, -1, GetDate()) AND GetDate()");
$show_lastweek = mysql_fetch_array($get_lastweek); ?>


<?php echo "<pre>" . print_r($show_lastweek) . "</pre>"; ?>
 

 

 

Edited by Zane
  • 11 months later...

I tried running the following as suggested as a SQL query directly in mySql and I get the following error

 

FUNCTION billing_accounts.DATEADD does not exist

 

Try this

<?php
$get_lastweek = mysql_query("SELECT SUM(total) FROM invoices WHERE date_created BETWEEN DATEADD(wk, -1, GetDate()) AND GetDate()");
$show_lastweek = mysql_fetch_array($get_lastweek); ?>


<?php echo "<pre>" . print_r($show_lastweek) . "</pre>"; ?>
 
Edited by Ihsaan

If you want to use php functions inside sql string, then the syntax should be different.

 

Try,

$get_lastweek = mysql_query('SELECT SUM(total) FROM invoices WHERE date_created BETWEEN DATEADD(wk, -1,'. GetDate().') AND '.GetDate());
Edited by jazzman1

I'd use

SELECT SUM(total) as total
FROM invoices 
WHERE date_created > CURDATE() - INTERVAL 7 DAY

but the function you were looking for is DATE_ADD() or DATE_SUB() (which is what you want in this instance)

Edited by Barand
$get_lastweek = mysql_query("SELECT SUM(total) as total FROM invoices WHERE date_created > CURDATE() - INTERVAL 7 DAY");

$show_lastweek = mysql_fetch_array("$get_lastweek");

 

<?php echo ($show_lastweek); ?>

 

When I insert the above into my code it returns nothing

Yes, answer the question when it's asked

 

 

First, define "last week".

 

Is that

  • during the previous calendar week (and is that Sun-Sat or Mon-Sun)
  • or the last 7 days

 

 

Then

$dt = new DateTime('last saturday');
$saturday = $dt->format('Y-m-d');
$sql = "SELECT SUM(total) as total
    FROM invoices 
    WHERE date_created BETWEEN '$saturday' - INTERVAL 6 DAY AND '$saturday' ";

 

$get_lastweek = mysql_query("SELECT SUM(total) as total FROM invoices WHERE date_created > CURDATE() - INTERVAL 7 DAY");
$show_lastweek = mysql_fetch_array("$get_lastweek");
 
<?php echo ($show_lastweek); ?>
 
When I insert the above into my code it returns nothing

 

 

should be

<?php echo ($show_lastweek['total']); ?>
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.