sprintlife Posted May 15, 2009 Share Posted May 15, 2009 Okay so a good friend did this for me /* $memorial = mysql_numrows($database->query("SELECT id FROM users WHERE userlevel ='0'")); $fhdirector = mysql_numrows($database->query("SELECT id FROM users WHERE userlevel ='1'")); $nonprofile = mysql_numrows($database->query("SELECT id FROM users WHERE userlevel ='2'")); $sales = mysql_numrows($database->query("SELECT id FROM users WHERE userlevel ='3'")); $support = mysql_numrows($database->query("SELECT id FROM users WHERE userlevel ='4'")); $admin = mysql_numrows($database->query("SELECT id FROM users WHERE userlevel ='5'")); */ $SQL = " SELECT SUM(IF(userlevel='0',1,0)) AS numMemorial, SUM(IF(userlevel='1',1,0)) AS numFHDirector, SUM(IF(userlevel='2',1,0)) AS numNonProfile, SUM(IF(userlevel='3',1,0)) AS numSales, SUM(IF(userlevel='4',1,0)) AS numSupport, SUM(IF(userlevel='5',1,0)) AS numAdmin FROM users"; $result = $database->query($SQL); foreach (mysql_fetch_assoc($result) as $key => $value) { $$key = intval($value); //force string to an integer value } I am trying to do the same thing but with this table /* $jan = mysql_numrows($database->query("SELECT id FROM memorial_page WHERE MONTH(FROM_UNIXTIME(timestamp)) = 01 AND YEAR(FROM_UNIXTIME(timestamp)) = $year")); $fen = mysql_numrows($database->query("SELECT id FROM memorial_page WHERE MONTH(FROM_UNIXTIME(timestamp)) = 02 AND YEAR(FROM_UNIXTIME(timestamp)) = $year")); $mar = mysql_numrows($database->query("SELECT id FROM memorial_page WHERE MONTH(FROM_UNIXTIME(timestamp)) = 03 AND YEAR(FROM_UNIXTIME(timestamp)) = $year")); $apr = mysql_numrows($database->query("SELECT id FROM memorial_page WHERE MONTH(FROM_UNIXTIME(timestamp)) = 04 AND YEAR(FROM_UNIXTIME(timestamp)) = $year")); $may = mysql_numrows($database->query("SELECT id FROM memorial_page WHERE MONTH(FROM_UNIXTIME(timestamp)) = 05 AND YEAR(FROM_UNIXTIME(timestamp)) = $year")); $jun = mysql_numrows($database->query("SELECT id FROM memorial_page WHERE MONTH(FROM_UNIXTIME(timestamp)) = 06 AND YEAR(FROM_UNIXTIME(timestamp)) = $year")); $jul = mysql_numrows($database->query("SELECT id FROM memorial_page WHERE MONTH(FROM_UNIXTIME(timestamp)) = 07 AND YEAR(FROM_UNIXTIME(timestamp)) = $year")); $aug = mysql_numrows($database->query("SELECT id FROM memorial_page WHERE MONTH(FROM_UNIXTIME(timestamp)) = 08 AND YEAR(FROM_UNIXTIME(timestamp)) = $year")); $sep = mysql_numrows($database->query("SELECT id FROM memorial_page WHERE MONTH(FROM_UNIXTIME(timestamp)) = 09 AND YEAR(FROM_UNIXTIME(timestamp)) = $year")); $out = mysql_numrows($database->query("SELECT id FROM memorial_page WHERE MONTH(FROM_UNIXTIME(timestamp)) = 10 AND YEAR(FROM_UNIXTIME(timestamp)) = $year")); $nov = mysql_numrows($database->query("SELECT id FROM memorial_page WHERE MONTH(FROM_UNIXTIME(timestamp)) = 11 AND YEAR(FROM_UNIXTIME(timestamp)) = $year")); $dec = mysql_numrows($database->query("SELECT id FROM memorial_page WHERE MONTH(FROM_UNIXTIME(timestamp)) = 12 AND YEAR(FROM_UNIXTIME(timestamp)) = $year")); */ $SQL2 = " SELECT SUM(IF(MONTH(FROM_UNIXTIME(timestamp))='01',1,0)) AND YEAR(FROM_UNIXTIME(timestamp)) = $year AS jan, SUM(IF(MONTH(FROM_UNIXTIME(timestamp))='02',1,0)) AND YEAR(FROM_UNIXTIME(timestamp)) = $year AS fen, SUM(IF(MONTH(FROM_UNIXTIME(timestamp))='03',1,0)) AND YEAR(FROM_UNIXTIME(timestamp)) = $year AS mar, SUM(IF(MONTH(FROM_UNIXTIME(timestamp))='04',1,0)) AND YEAR(FROM_UNIXTIME(timestamp)) = $year AS apr, SUM(IF(MONTH(FROM_UNIXTIME(timestamp))='05',1,0)) AND YEAR(FROM_UNIXTIME(timestamp)) = $year AS may, SUM(IF(MONTH(FROM_UNIXTIME(timestamp))='06',1,0)) AND YEAR(FROM_UNIXTIME(timestamp)) = $year AS jun, SUM(IF(MONTH(FROM_UNIXTIME(timestamp))='07',1,0)) AND YEAR(FROM_UNIXTIME(timestamp)) = $year AS jul, SUM(IF(MONTH(FROM_UNIXTIME(timestamp))='08',1,0)) AND YEAR(FROM_UNIXTIME(timestamp)) = $year AS aug, SUM(IF(MONTH(FROM_UNIXTIME(timestamp))='09',1,0)) AND YEAR(FROM_UNIXTIME(timestamp)) = $year AS sep, SUM(IF(MONTH(FROM_UNIXTIME(timestamp))='10',1,0)) AND YEAR(FROM_UNIXTIME(timestamp)) = $year AS out, SUM(IF(MONTH(FROM_UNIXTIME(timestamp))='11',1,0)) AND YEAR(FROM_UNIXTIME(timestamp)) = $year AS nov, SUM(IF(MONTH(FROM_UNIXTIME(timestamp))='12',1,0)) AND YEAR(FROM_UNIXTIME(timestamp)) = $year AS dec FROM memorial_page"; $result2 = $database->query($SQL2); foreach (mysql_fetch_assoc($result2) as $key => $value) { // The error message below is talking about this line $$key = intval($value); //force string to an integer value } If anyone can example to me what I am doing wrong is. Here is the error message I get [15-May-2009 03:48:34] PHP Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/tyslo/public_html/file/panel/charts/admin.php on line 102 [15-May-2009 03:48:34] PHP Warning: Invalid argument supplied for foreach() in /home/tyslo/public_html/file/panel/charts/admin.php on line 102 So if anyone can help me out that would be great. Quote Link to comment https://forums.phpfreaks.com/topic/158235-trying-to-do-a-sums-and-if-statement-in-query/ Share on other sites More sharing options...
sprintlife Posted May 15, 2009 Author Share Posted May 15, 2009 I did this to fix it... SELECT SUM(MONTH(FROM_UNIXTIME(timestamp)) = '01' AND YEAR(FROM_UNIXTIME(timestamp)) = $year) AS numjan, SUM(MONTH(FROM_UNIXTIME(timestamp)) = '02' AND YEAR(FROM_UNIXTIME(timestamp)) = $year) AS numfen, SUM(MONTH(FROM_UNIXTIME(timestamp)) = '03' AND YEAR(FROM_UNIXTIME(timestamp)) = $year) AS nummar, SUM(MONTH(FROM_UNIXTIME(timestamp)) = '04' AND YEAR(FROM_UNIXTIME(timestamp)) = $year) AS numapr, SUM(MONTH(FROM_UNIXTIME(timestamp)) = '05' AND YEAR(FROM_UNIXTIME(timestamp)) = $year) AS nummay, SUM(MONTH(FROM_UNIXTIME(timestamp)) = '06' AND YEAR(FROM_UNIXTIME(timestamp)) = $year) AS numjun, SUM(MONTH(FROM_UNIXTIME(timestamp)) = '07' AND YEAR(FROM_UNIXTIME(timestamp)) = $year) AS numjul, SUM(MONTH(FROM_UNIXTIME(timestamp)) = '08' AND YEAR(FROM_UNIXTIME(timestamp)) = $year) AS numaug, SUM(MONTH(FROM_UNIXTIME(timestamp)) = '09' AND YEAR(FROM_UNIXTIME(timestamp)) = $year) AS numsep, SUM(MONTH(FROM_UNIXTIME(timestamp)) = '10' AND YEAR(FROM_UNIXTIME(timestamp)) = $year) AS numout, SUM(MONTH(FROM_UNIXTIME(timestamp)) = '11' AND YEAR(FROM_UNIXTIME(timestamp)) = $year) AS numnov, SUM(MONTH(FROM_UNIXTIME(timestamp)) = '12' AND YEAR(FROM_UNIXTIME(timestamp)) = $year) AS numdec FROM memorial_page If you guys know any better way to do it I would love to hear it. Quote Link to comment https://forums.phpfreaks.com/topic/158235-trying-to-do-a-sums-and-if-statement-in-query/#findComment-834610 Share on other sites More sharing options...
sprintlife Posted May 15, 2009 Author Share Posted May 15, 2009 How do I mark this solved? Quote Link to comment https://forums.phpfreaks.com/topic/158235-trying-to-do-a-sums-and-if-statement-in-query/#findComment-834611 Share on other sites More sharing options...
Ken2k7 Posted May 15, 2009 Share Posted May 15, 2009 Click on the bottom left link called Topic Solved. It's above the quick reply. Quote Link to comment https://forums.phpfreaks.com/topic/158235-trying-to-do-a-sums-and-if-statement-in-query/#findComment-834737 Share on other sites More sharing options...
fenway Posted May 15, 2009 Share Posted May 15, 2009 Yikes... why not just GROUP BY month? Quote Link to comment https://forums.phpfreaks.com/topic/158235-trying-to-do-a-sums-and-if-statement-in-query/#findComment-834917 Share on other sites More sharing options...
sprintlife Posted May 15, 2009 Author Share Posted May 15, 2009 What do you mean fanway? Quote Link to comment https://forums.phpfreaks.com/topic/158235-trying-to-do-a-sums-and-if-statement-in-query/#findComment-834992 Share on other sites More sharing options...
fenway Posted May 19, 2009 Share Posted May 19, 2009 Maybe I'm not understanding what you're trying to "count". Quote Link to comment https://forums.phpfreaks.com/topic/158235-trying-to-do-a-sums-and-if-statement-in-query/#findComment-837168 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.