kapkap Posted September 5, 2014 Share Posted September 5, 2014 Hi there I am new to this forum and not much of a php expert. I hope you can assist me. My problem is as follows: I have a column consisting of several values (in this case: sceience, art, humanistics and music) I would like to use the following mysqli query in order to count the appearances of each value in this column: $query = "SELECT SUM (IF(department = 'science', 1, 0)) AS science, SUM(IF(department = 'art', 1, 0))-> AS art FROM new2" I have tried lots of ways to loop the results but couldn't get the it working. Can anyone suggest any idea? Thanks Hanan Link to comment https://forums.phpfreaks.com/topic/290869-getting-result-from-a-select-sum-if-query/ Share on other sites More sharing options...
jazzman1 Posted September 5, 2014 Share Posted September 5, 2014 $query = "SELECT SUM (IF(department = 'science', 1, 0)) AS science, SUM(IF(department = 'art', 1, 0))-> AS art FROM new2" Is this the query string trying to execute by php to mysql? if so, that the syntax of your query is not correct. Link to comment https://forums.phpfreaks.com/topic/290869-getting-result-from-a-select-sum-if-query/#findComment-1490037 Share on other sites More sharing options...
cyberRobot Posted September 5, 2014 Share Posted September 5, 2014 Try changing this: $query = "SELECT SUM (IF(department = 'science', 1, 0)) AS science, SUM(IF(department = 'art', 1, 0))-> AS art FROM new2"; To this: $query = "SELECT SUM (IF(department = 'science', 1, 0)) AS science, SUM(IF(department = 'art', 1, 0)) AS art FROM new2"; Note that I removed the "->" before " AS art". Link to comment https://forums.phpfreaks.com/topic/290869-getting-result-from-a-select-sum-if-query/#findComment-1490038 Share on other sites More sharing options...
jazzman1 Posted September 5, 2014 Share Posted September 5, 2014 SUM (IF(department = 'science', 1, 0)) Remove the empty space between sum and (). Sum is a function in mysql Link to comment https://forums.phpfreaks.com/topic/290869-getting-result-from-a-select-sum-if-query/#findComment-1490049 Share on other sites More sharing options...
cyberRobot Posted September 5, 2014 Share Posted September 5, 2014 Remove the empty space between sum and (). Sum is a function in mysql For some reason I thought the space didn't matter, but you're right. I must have removed it subconsciously when I ran my tests. Link to comment https://forums.phpfreaks.com/topic/290869-getting-result-from-a-select-sum-if-query/#findComment-1490050 Share on other sites More sharing options...
kapkap Posted September 5, 2014 Author Share Posted September 5, 2014 Thanks any advice regarding the display of this query, I have tried so many and got nothing. Thanks Link to comment https://forums.phpfreaks.com/topic/290869-getting-result-from-a-select-sum-if-query/#findComment-1490052 Share on other sites More sharing options...
jazzman1 Posted September 5, 2014 Share Posted September 5, 2014 what debugging steps have you taken so far? A chance to see some of your actual code? Link to comment https://forums.phpfreaks.com/topic/290869-getting-result-from-a-select-sum-if-query/#findComment-1490053 Share on other sites More sharing options...
kapkap Posted September 5, 2014 Author Share Posted September 5, 2014 hi solved with help from a friend, a little different but gives me the data I need $query = "select department, count(department) as count from new2 group by department"; if ($result = mysqli_query($con, $query)) { while ($row = mysqli_fetch_assoc($result)) { printf ("%s (%s)\n", $row['department'], $row['count']); } /* free result set */ mysqli_free_result($result); } mysqli_close($con); thank you all Link to comment https://forums.phpfreaks.com/topic/290869-getting-result-from-a-select-sum-if-query/#findComment-1490054 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.