简体   繁体   English

PHP&MySQL:使用php for和mysql查询的问题

[英]PHP&MySQL: issue using php for and mysql query

I would calculate the total profit of a DataBase for each month using 12 different function [one for each month(yes its probably not a nice solution but I am learning step by step )] 我将使用12个不同的函数计算每个月每个数据库的总利润[每个月一个(是的,这可能不是一个很好的解决方案,但我正在逐步学习)]

This is the .php code (x12 times) 这是.php代码(x12次)

$January="SELECT JanuaryProfit()";     //Function
echo "<b>January Profit: E</b>";
$January = mysql_query($giugno,$conn)
          or die("Query failed: " . mysql_error($conn));
$num_rows=mysql_num_rows($January);

if (! $num_rows)
      echo "<p>0</p>";
else {
      while ($row = mysql_fetch_row($January)) {
            $price=$row[0];
            echo "$price";
      };
}

Function JanuaryProfit() is declared this way [MySQL]: 函数JanuaryProfit()以此方式声明[MySQL]:

BEGIN
DECLARE PriceTOT FLOAT;
SELECT SUM(o.PrezzoTot) INTO PriceTOT
FROM orders o
WHERE o.Data BETWEEN '2012-01-01' AND '2012-01-31';
RETURN PriceTOT;
END

And so I've declared singularly february,march,...,december. 因此,我已经将2月,3月,...定为12月。

I wouldn't paste 12 times the first code into the .php page.I've tried using a for cicle that use different function every time but i cannot figure how to solve this,correctly.Any suggest? 我不会将第一个代码粘贴12次到.php页面。我尝试使用每次使用不同功能的for cicle,但是我无法弄清楚如何正确地解决此问题。有人建议吗?

All I wanna do by now is changing the php part only,using a loop. 我现在想做的就是仅使用循环更改php部分。

Oh my dear, sweet, flying spaghetti monster. 噢,亲爱的,甜美的,飞起来的意大利面怪物。 Stop whatever you are doing, then burn it, and then use something like the below to avoid writing a function for every month of every year from the beginning of time until the end. 停止您正在做的任何事情,然后刻录它,然后使用如下所示的方法来避免从开始到结束的每年的每个月编写函数。

SELECT 
  YEAR(o.Data) as 'year',
  MONTH(o.Data) as 'month',
  SUM(o.PrezzoTot) as 'total'
FROM orders o
GROUP BY YEAR(o.Data), MONTH(o.Data)

I assume at some point you were having difficulty figuring out the last day of the month programmatically, so here's another link to LASTDAY() which is also the documentation page for all of the massively useful date functions. 我认为在某些时候您很难以编程方式计算出该月的最后一天,所以这是LASTDAY()的另一个链接,它也是所有大量有用的日期函数的文档页面。


edit 编辑

define('masochism', TRUE);

$ohgodwhy = array('January','February','March','April','May','June','July','August','September','October','November','December');

$aaahhh = 'SELECT %sProfit()';

foreach( $ohgodwhy as $pleaseno ) {
    $itburns = sprintf($aaahhh, $pleaseno);
    $hidethebodies = mysql_query($itburns) or die("Couldn't hide the bodies because: " . mysql_error());
    // etc...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM