adicrst Posted November 26, 2007 Share Posted November 26, 2007 i have a database and i want to output the total of users,bani(money) and pucte(points) i made this script <?php $con = mysql_connect("localhost","root",""); if (!$con) { die ('Eroare conectare'.mysql_error()); } //selectarea bazei de date mysql_select_db("rpg",$con); //extragerea datelor $result = mysql_query("SELECT * FROM user"); echo "<b>Membrii | Bani | Puncte</b><br>"; while($row = mysql_fetch_array($result)) { echo $row['username']." ".$row['bani']." ".$row['puncte']."<br>"; } //total members $membri = mysql_query("SELECT MAX(iduser) FROM user"); while($row_m=mysql_fetch_array($membri)) { echo "<br><br>Total membri: ".$row_m[0]; } //total money $bani = mysql_query("SELECT SUM(bani) FROM user"); while($row_b=mysql_fetch_array($bani)) { echo "<br>Total bani: ".$row_b[0]; } //total points $puncte = mysql_query("SELECT SUM(puncte) FROM user"); while($row_p=mysql_fetch_array($puncte)) { echo "<br>Total puncte: ".$row_p[0]; } mysql_close($con); ?> the output is like this Membrii | Bani | Puncte adicrst 100 150 adi 150 150 alin 100 150 Total membri: 3 Total bani:350 Total puncte: 450 now i want to sum Total bani: 350 with Total puncte: 450 => 800. How do i do this ? Quote Link to comment Share on other sites More sharing options...
Distant_storm Posted November 26, 2007 Share Posted November 26, 2007 can in this bit you not put $running_total=0; while($row = mysql_fetch_array($result)) { echo $row['username']." ".$row['bani']." ".$row['puncte']."<br>"; $running_total= $running_total + $row['bani']; $running_total = $running_total + $row['puncte']; } and at the end just echo total ??????? declaire $running total as 0 then it goes through each member of the table adding all their bani and puncte together. Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted November 26, 2007 Share Posted November 26, 2007 You could do that all in one query <?php $query = "SELECT MAX(iduser) as max_user, SUM(bani) as max_bani, SUM(puncte) as max_puncte, (puncte + bani) as total FROM users"; $result = mysql_query($query)or die(mysql_error()); $row = mysql_fetch_assoc($result); print<<<HERE Total membri: {$row['max_user']}<br> Total bani: {$row['max_bani']}<br> Total puncte: {$row['max_puncte']}<br> Total bani and puncte: {$row['total']} HERE; ?> Quote Link to comment Share on other sites More sharing options...
adicrst Posted November 26, 2007 Author Share Posted November 26, 2007 i tried this <?php $con = mysql_connect("localhost","root",""); if (!$con) { die ('Eroare conectare'.mysql_error()); } //selectarea bazei de date mysql_select_db("rpg",$con); $query = "SELECT MAX(iduser) as max_user, SUM(bani) as max_bani, SUM(puncte) as max_puncte, (puncte + bani) as total FROM users"; $result = mysql_query($query)or die(mysql_error()); $row = mysql_fetch_assoc($result); print<<<HERE Total membri: {$row['max_user']}<br> Total bani: {$row['max_bani']}<br> Total puncte: {$row['max_puncte']}<br> Total bani and puncte: {$row['total']} HERE; ?> and i get Table 'rpg.users' doesn't exist also with my old code i tried <?php $con = mysql_connect("localhost","root",""); if (!$con) { die ('Eroare conectare'.mysql_error()); } //selectarea bazei de date mysql_select_db("rpg",$con); //extragerea datelor $result = mysql_query("SELECT * FROM user"); echo "<b>Membrii | Bani | Puncte</b><br>"; while($row = mysql_fetch_array($result)) { echo $row['username']." ".$row['bani']." ".$row['puncte']."<br>"; } //total membri $membri = mysql_query("SELECT MAX(iduser) FROM user"); while($row_m=mysql_fetch_array($membri)) { echo "<br><br>Total membri: ".$row_m[0]; } //total bani $bani = mysql_query("SELECT SUM(bani) FROM user"); while($row_b=mysql_fetch_array($bani)) { echo "<br>Total bani: ".$row_b[0]; } //total puncte $puncte = mysql_query("SELECT SUM(puncte) FROM user"); while($row_p=mysql_fetch_array($puncte)) { echo "<br>Total puncte: ".$row_p[0]; } //total puncte si bani $total=$row_b[0]+$row_p[0]; echo "<br><br>bani + puncte = ".$total; mysql_close($con); ?> and the output is Membrii | Bani | Puncte adicrst 100 150 adi 150 150 alin 100 150 Total membri: 3 Total bani:350 Total puncte: 450 bani + puncte = 0 Quote Link to comment Share on other sites More sharing options...
adicrst Posted November 26, 2007 Author Share Posted November 26, 2007 sorry for the double post, but i realized that at my own code the variables $row are not global and i dont know how to make it. @pocobueno1388: i get this error Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted November 26, 2007 Share Posted November 26, 2007 <?php $query = "SELECT MAX(iduser) as max_user, SUM(bani) as max_bani, SUM(puncte) as max_puncte, (puncte + bani) as total FROM user GROUP BY iduser"; $result = mysql_query($query)or die(mysql_error()); $row = mysql_fetch_assoc($result); print<<<HERE Total membri: {$row['max_user']}<br> Total bani: {$row['max_bani']}<br> Total puncte: {$row['max_puncte']}<br> Total bani and puncte: {$row['total']} HERE; ?> Quote Link to comment Share on other sites More sharing options...
adicrst Posted November 26, 2007 Author Share Posted November 26, 2007 it works, thank you. Can u say what is wrong in my code and why is not working ? Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted November 26, 2007 Share Posted November 26, 2007 You were just using way more queries than you had to. When you got this error Table 'rpg.users' doesn't exist Thats pretty straight-forward that I accidentally made your table name "user" plural. With your original code, all you had to do was add the two rows up...but even if you did that and it worked, the way you were doing it with all the queries isn't very efficient as you can see I gave you code that does in a lot less lines and is much easier on the database. 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.