imgrooot Posted April 13, 2020 Share Posted April 13, 2020 I have a table with "votes" column. I basically want to retrieve the sum of all the votes columns associated with a user. This is the full code. It gives me "NULL" output. $count_votes = $db->prepare("SELECT SUM(votes) as total FROM entries WHERE user_id = :user_id"); $count_votes->bindParam(':user_id', $user_id); $count_votes->execute(); $t_votes = $count_votes->fetchColumn(); $get_votes = $t_votes['total']; echo $get_votes; But if I remove the user_id bind paramter, it gives me the results. But I need that parameter to show which user has how many votes. $count_votes = $db->prepare("SELECT SUM(votes) as total FROM entries"); $count_votes->execute(); $t_votes = $count_votes->fetchColumn(); $get_votes = $t_votes['total']; echo $get_votes; What am I doing wrong? Quote Link to comment https://forums.phpfreaks.com/topic/310563-counting-the-sum-of-a-column-not-working/ Share on other sites More sharing options...
Barand Posted April 13, 2020 Share Posted April 13, 2020 I find it difficult to believe that either of those code snippets work. If you are using fetchColumn() the total that you want will be in $t_votes. (It will not be an array, just the value from the fetched column) Quote Link to comment https://forums.phpfreaks.com/topic/310563-counting-the-sum-of-a-column-not-working/#findComment-1576792 Share on other sites More sharing options...
imgrooot Posted April 13, 2020 Author Share Posted April 13, 2020 41 minutes ago, Barand said: I find it difficult to believe that either of those code snippets work. If you are using fetchColumn() the total that you want will be in $t_votes. (It will not be an array, just the value from the fetched column) I just realized what the issue was. The votes in the table have a different user_id than what I am currently logged in as. So naturally it will not return any results. Here's a new code based on your suggestion. Works fine. $count_votes = $db->prepare("SELECT SUM(votes) FROM entries WHERE user_id = :user_id"); $count_votes->bindParam(':user_id', $user_id); $count_votes->execute(); $get_votes = $count_votes->fetchColumn(); Quote Link to comment https://forums.phpfreaks.com/topic/310563-counting-the-sum-of-a-column-not-working/#findComment-1576793 Share on other sites More sharing options...
Phi11W Posted April 14, 2020 Share Posted April 14, 2020 19 hours ago, imgrooot said: I want to retrieve the sum of all the votes columns associated with a user. This is the full code. It gives me "NULL" output. If the user has never voted, then all of the individual votes values will be NULL. If all of the values given to SUM() are NULL, then the result is also NULL. You may want to add code to your query to handle this case. I think ... IFNULL( SUM( votes ), 0 ) ... will do what you want. Regards, Phill W. 1 Quote Link to comment https://forums.phpfreaks.com/topic/310563-counting-the-sum-of-a-column-not-working/#findComment-1576816 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.