Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can simplify this task by letting MySQL do more of the work for you.</p> <p>You have an array <code>$receivers</code> which presumably contains id's for subscribers to (receivers of) something. (I may use receiver and subscriber interchangeably.)</p> <p>The <code>status()</code> function retrieves from the database the <code>value</code> for each receiver where the <code>subscription_end_date</code> is null. I am going to assume that there is only one row per subscriber because you only return the last result via the variable <code>$rowservice</code>.</p> <p>It's unclear from your code if you want a count of receivers with a <code>value</code> other than 0, or if you want a sum of all non-zero values. The question then is:</p> <ul> <li>Do you want to know how many subscribers have nonzero values? or,</li> <li>Do you want to know the sum of all values for subscribers with nonzero values?</li> </ul> <p>Fortunately both of these can be easily found:</p> <p><strong>How many receivers have non-zero values?</strong></p> <pre><code>$allreceivers = implode(',', $receivers); $query = "SELECT COUNT(id) AS total_receivers FROM user WHERE subscription_end_date IS NULL AND value != 0 AND id IN ($allreceivers);"; $result = mysql_query($query, $d1); $row = mysql_fetch_array($result); $total_receivers = $row['total_receivers']; echo "The number of receivers having status (value) other than 0 is $total_receivers"; </code></pre> <p><strong>What is the sum of all receiver values?</strong></p> <pre><code>$allreceivers = implode(',', $receivers); $query = "SELECT SUM(value) AS receiver_value_sum FROM user WHERE subscription_end_date IS NULL AND id IN ($allreceivers);"; $result = mysql_query($query, $d1); $row = mysql_fetch_array($result); $receiver_value_sum = $row['receiver_value_sum']; echo "The sum of receiver values is $receiver_value_sum"; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload