aquatradehub Posted March 16, 2014 Share Posted March 16, 2014 Hi, I have 2 functions which are meant to return the number of items a user has for sale. These are function count_items_listed($user_id) { return mysql_result(mysql_query("SELECT COUNT(`items_listed`) FROM `user_stats` WHERE `user_id` = $user_id"), 0); } function count_live_items_listed($user_id) { return mysql_result(mysql_query("SELECT COUNT(`live_items_listed`) FROM `user_stats` WHERE `user_id` = $user_id"), 0); } To view this information, I am using $count_items_listed = count_items_listed($user_id); AND $count_live_items_listed = count_live_items_listed($user_id); <p>Number of live items currently listed: <?php echo $count_live_items_listed; ?></p> <p>Number of items currently listed: <?php echo $count_items_listed; ?></p> The database holds three columns which contain the following test informationuser_id, items_listed, live_items_listed12 1 012 0 112 1 0But when I use:<p>Number of live items currently listed: <?php echo $count_live_items_listed; ?></p><p>Number of items currently listed: <?php echo $count_items_listed; ?></p>The results for both show a result of 3, instead of live_items_listed being equal to 2 and items_listed equal to 1.Any help is much appreciatedPaul Quote Link to comment Share on other sites More sharing options...
requinix Posted March 16, 2014 Share Posted March 16, 2014 COUNT() will count non-null values. Not distinct values. Either you want a condition WHERE items_listed = 1 or you want a SUM() of the values, it depends how those values are managed. Quote Link to comment 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.