Ik hadt een vraagje. Ik probeer punten op te tellen die uit een mysql DB worden gehaald. 1comp staan 2 rijen is nu gaat hij niet optellen maar pakt hij alleen de punten van de laatste persoon in de DB dus hij telt niks op.
Ik denk dat de fout zit met de functie SUM die ik ik gebruik, alleen ik kan niet vinden wat.

Mijn code:

<?php
   
    $conn = mysql_connect("$host", "$user", "$pass");
   
   if (!$conn) {
       echo "Kan geen verbinding maken met de DB: " . mysql_error();
       exit;
   }
   
   if (!mysql_select_db("$db")) {
       echo "Kan mydbname niet selecteren: ";
   }
   
   $sql = "SELECT naam, sum(1comp) AS totaal, 1comp, sum(2comp) AS totaal2, 2comp, sum(beker) AS totaal3, beker, sum(oefen) AS totaal4, oefen
           FROM  topscorers
		   GROUP BY naam";          

		   


   $result = mysql_query($sql);

   if (!$result) {
       echo "Kon de query ($sql) niet uitvoeren: " . mysql_error();
       exit;
   }
   
   if (mysql_num_rows($result) == 0) {
       echo "Geen rijen gevonden, niets te printen, dus stoppen.";
       exit;
   }
  
   while ($row = mysql_fetch_assoc($result)) {
   
$echo1 = $row['totaal'];

      ?>
	  
<TD width=140 bgColor=#ffffff height=20><font size="1" face="Verdana"><?php echo "". $row['naam']; ?></font></TD>
<TD align=middle width=31 height=20><font size="1" face="Verdana"><?php echo "". $row['1comp']; ?></font></TD>
<TD align=middle width=38 height=20><font size="1" face="Verdana"><?php echo "". $row['2comp']; ?></font></TD>
<TD align=middle width=55 height=20><font size="1" face="Verdana"><?php echo "". $row['beker']; ?></font></TD>
<TD align=middle width=60 height=20><font size="1" face="Verdana"><?php echo "". $row['oefen']; ?></font></TD>
<TD align=middle width=80 height=20><B><font size="1" face="Verdana">34</font></B></TD></TR>
<?php  
} 
?>
<TR>
<TD width=140 bgColor=#ffffff height=20></TD>
<TD align=middle width=69 colSpan=2 height=20></TD>
<TD align=middle width=55 height=20></TD>
<TD align=middle width=60 height=20></TD>
<TD align=middle width=80 height=20></TD></TR>
<TR>
<TD width=140 bgColor=#ffb400 height=20><font size="1" face="Verdana">Totaal</font></TD>
<TD align=middle width=31 bgColor=#ffb400 height=20><font size="1" face="Verdana"><?php echo $echo1 ?></font></TD>
<TD align=middle width=38 bgColor=#ffb400 height=20><font size="1" face="Verdana"><?php echo "". $row['totaal2']; ?></font></TD>
<TD align=middle width=55 bgColor=#ffb400 height=20><font size="1" face="Verdana"><?php echo "". $row['totaal3']; ?></font></TD>
<TD align=middle width=60 bgColor=#ffb400 height=20><font size="1" face="Verdana"><?php echo "". $row['totaal4']; ?></font></TD>
<TD align=middle width=80 bgColor=#ffb400 
height=20><B><font size="1" face="Verdana">122</font></B></TD></TR></TBODY></TABLE><font size="1" face="Verdana"><BR></font>
</p>  
<?php
	 mysql_free_result($result);   
?> 


Ik heb al gedaan:

-Veel getest
-Op forums gekijken wat over de functie SUM ging.

Ik hoop dat iemand mijn kan helpen
Ik heb ook ooit zoiets gehad, en ik heb het zo opgelost:

<?php

$select = "SELECT SUM(waarde) AS variabele FROM tabel";
$query = mysql_query($select);
$punten = (mysql_result($query,"variabele"));

?>

Hier worden alle nummers in het veld 'waarde' van de tabel 'tabel' opgeteld en op opgeslagen als $punten.

Succes ermee!
Ik ga het proberen, bedankt al vast!

-edit-

werkt helaas niet.
De query is niet correct, SUM() is een agregate functie waardoor je de GROUP BY dus correct moet noteren. Zie de tutorials voor nadere uitleg.
Ik heb hem nog eens goed gelezen ik snap hem al beter. Alleen nu telt hij het wel op maar ik krijg een dubbele regel.

code nu:

<?php
$sql = "SELECT naam, sum(1comp) AS totaal, 1comp, sum(2comp) AS totaal2, 2comp, sum(beker) AS totaal3, beker, sum(oefen) AS totaal4, oefen
FROM topscorers
GROUP BY 1comp WITH ROLLUP";
?>

De rij met de hoogste punten aantal zet hij dubel onder aan. En dan de dubele de laatste rij staat geen cijfer.

Al vast bedankt:)
Niemand?
<?php
$sql = "SELECT
naam,
sum(1comp) AS totaal,
sum(2comp) AS totaal2,
sum(beker) AS totaal3,
sum(oefen) AS totaal4
FROM topscorers
GROUP BY
naam";
?>
Werkt helaas niet. Dan telt hij alleen de laatste rij.
staat er dan maar één naam in je tabel 'topscorers'?

edit: of lees je het resultaat verkeerd uit, dat je alleen de laatste regel weergeeft?
$echo1 = $row['totaal'];

Dit staat in de while en de echo staat ná de while. Dus logisch dat hier het totaal in zit van het laatst gevonden record.
Ik heb nu dit:

<?php
   
    $conn = mysql_connect("$host", "$user", "$pass");
   
   if (!$conn) {
       echo "Kan geen verbinding maken met de DB: " . mysql_error();
       exit;
   }
   
   if (!mysql_select_db("$db")) {
       echo "Kan mydbname niet selecteren: ";
   }
   
   $sql = "SELECT id, naam, sum(1comp) AS totaal, 1comp, sum(2comp) AS totaal2, 2comp, sum(beker) AS totaal3, beker, sum(oefen) AS totaal4, oefen
           FROM  topscorers
           GROUP BY id  WITH ROLLUP";          

           


   $result = mysql_query($sql);

   if (!$result) {
       echo "Kon de query ($sql) niet uitvoeren: " . mysql_error();
       exit;
   }
   
   if (mysql_num_rows($result) == 0) {
       echo "Geen rijen gevonden, niets te printen, dus stoppen.";
       exit;
   }
  
   while ($row = mysql_fetch_assoc($result)) {
   
$echo1 = $row['totaal'];
$echo2 = $row['totaal2'];
$echo3 = $row['totaal3'];
$echo4 = $row['totaal4'];

$optellen = $row['1comp'] + $row['2comp'] + $row['beker'] + $row['oefen'];

      ?>
      
<TD width=140 bgColor=#ffffff height=20><font size="1" face="Verdana"><?php echo "". $row['naam']; ?></font></TD>
<TD align=middle width=31 height=20><font size="1" face="Verdana"><?php echo "". $row['1comp']; ?></font></TD>
<TD align=middle width=38 height=20><font size="1" face="Verdana"><?php echo "". $row['2comp']; ?></font></TD>
<TD align=middle width=55 height=20><font size="1" face="Verdana"><?php echo "". $row['beker']; ?></font></TD>
<TD align=middle width=60 height=20><font size="1" face="Verdana"><?php echo "". $row['oefen']; ?></font></TD>
<TD align=middle width=80 height=20><B><font size="1" face="Verdana"><?php echo $optellen; ?></font></B></TD></TR>
<?php  
} 
?>
<TR>
<TD width=140 bgColor=#ffffff height=20></TD>
<TD align=middle width=69 colSpan=2 height=20></TD>
<TD align=middle width=55 height=20></TD>
<TD align=middle width=60 height=20></TD>
<TD align=middle width=80 height=20></TD></TR>
<TR>
<TD width=140 bgColor=#ffb400 height=20><font size="1" face="Verdana">Totaal</font></TD>
<TD align=middle width=31 bgColor=#ffb400 height=20><font size="1" face="Verdana"><?php echo $echo1 ?></font></TD>
<TD align=middle width=38 bgColor=#ffb400 height=20><font size="1" face="Verdana"><?php echo $echo2 ?></font></TD>
<TD align=middle width=55 bgColor=#ffb400 height=20><font size="1" face="Verdana"><?php echo $echo3 ?></font></TD>
<TD align=middle width=60 bgColor=#ffb400 height=20><font size="1" face="Verdana"><?php echo $echo4 ?></font></TD>
<TD align=middle width=80 bgColor=#ffb400 
height=20><B><font size="1" face="Verdana"><?php echo $optellen; ?></font></B></TD></TR></TBODY></TABLE><font size="1" face="Verdana"><BR></font>
</p>  
<?php
     mysql_free_result($result);   
?> 

Nu werkt het alleen de laatste rij weergeeft hij dubbel. (met de meeste punten) Dit wil ik niet.

Voorbeeld: http://irene58d1.nl/toplijst2.php

De rij met de naam: dsf is dubbel. (Staat maar 1x in db)

Reageren