Wednesday 15 January 2014

php - Getting total in while statement with UNION query -


I am trying to calculate the earnings of a user, that is reflected on the user's home page so that they Know how much their referrals are $ get_ref_stats = $ db- & gt; Query ("Select * FROM member 'ou' referral` = '' '')" $ user_info ['username'] .. "; $ Total_cash = 0; While $ ref_stats = $ get_ref_stats-> gt; Fetch_assoc ()) {$ get_ref_cash = $ db- & gt; Query ("Select * FROM was completed 'WHERE' user` = '". $ Ref_stats [' username ']. "Choose union *` full_arpat` where' user '=' ". $ Ref_stats ['user Name ']. "'"); $ Countr_cash = $ get_ref_cash-> gt; Fetch_assoc (); $ Total_cash + = $ countr_cash ['Cash']; $ Countr_c_rate = $ setting_info ['ref_rate'] * 0.01; $ Total_cash = $ total_cash * $ countr_c_rate; }

This works fine when I just

  $ get_ref_cash = $ db-> query ("select" to 'full' where 'user' = '". $ Ref_stats [' Username ']."' ');   

But as soon as I added to union , it is now correctly calculated For example, there is 1 entry in complete and entry for cash 0.75 entry in both of these entries is a entry in complete_trapet . The variable for $ countr_c_rate is 0.10, so $ total_cash should be equal to 0.15 but instead it is displayed without UNION and without 0.075, as it works

I hope this makes sense because I was not sure how to explain this issue, but I am very unsure what I have done here.

UNION ALL Since UNION in your second query instead of class = "post-text" itemprop = "text">

UNION Eliminates duplicate Rinamon That is why you 0.05 instead of 0.15 received.

Now, instead of hitting your database multiple times with the client code, you can calculate the total calculation of your cash

This may be incorrect without seeing your table structures and sample data This query may look like this

  SELECT SUM (cash) from Cash_total (choose from c.cash c Meet Jose members c.user = M.username WHERE m.referral =? Unius select all r.cash Join the full_ repeat r without the statement made r.user = m.username WHERE m on member M Ripe php code  
  $ sql =   

"SELECT SUM (cash) cash_total FROM (Select C.cash full CJoint member on M. c.user = m.username WHERE m.referral = '$ user_info [' username '] Select union all r.cash full_ repeat Join r members r.user = m Username WHERE m.referral = '$ user_info [' username ']') q "; $ result = $ db-> query ($ sql); if (! $ Result) {dead ($ db-> error ( )); // TODO: Better error handling} ($ line = $ result-> Fetch_assoc ()) {$ total_cash = $ row ['cash_total'] * $ setting_info ['ref_rate']; }

Note one side: Instead of creating questions with concatenation, use in mysqli this escal- Weak for injection.

No comments:

Post a Comment