Rila_xp Posted July 19, 2012 Report Share Posted July 19, 2012 (edited) Am tabelul statistics cu id,date si vizite.Vreau sa imi afiseze numarul total de vizite din ultimele 4 zile de exemplu si am incercat urmatorul cod si nu mi-a mers.Ce gresesc?$query = mysql_query("SELECT SUM(vizite) FROM statistics ORDER BY date DESC LIMIT 0,4", $con);// Print out resultwhile($rowz = mysql_fetch_array($query)){ $sumz = $rowz['SUM(vizite)'];}echo "This week $sumz <br>"; Edited July 19, 2012 by Rila_xp Quote Link to comment Share on other sites More sharing options...
andrew-46 Posted July 19, 2012 Report Share Posted July 19, 2012 mysql_error ce zice? Sau de ce mai exact nu merge codul? Ce valoare returneaza?+Ar fi bine daca ai posta si structura tabelului 'statistics'. Quote Link to comment Share on other sites More sharing options...
Sim Master Posted July 19, 2012 Report Share Posted July 19, 2012 Incearca cu asta, presupunand ca ai cate un rand pentru fiecare zi (asa am inteles din ce am citit)$sql = mysql_query('SELECT SUM(`vizite`) as `total_vizite` FROM `statistics` ORDER BY `id` DESC LIMIT 4') or die (mysql_error());$fetch = mysql_fetch_array($sql);echo $fetch[total_vizite]; Quote Link to comment Share on other sites More sharing options...
Rila_xp Posted July 19, 2012 Author Report Share Posted July 19, 2012 ^^Nu imi da nici o eroare,doar ca imi returneaza o valoare gresita(in loc sa imi ia ultimele 4 zile si sa imi adune,imi aduna pe toate zilele)^la fel face si codul dat de tineStructura tabelului am spus de la inceput,tabelul statistics cu campurile id,date,vizite pentru fiecare zi am un nou rand care contine id data si numarul de vizite Quote Link to comment Share on other sites More sharing options...
Sim Master Posted July 19, 2012 Report Share Posted July 19, 2012 Dar campul "vizite" este de tip INT sau altul care accepta doar numere? Quote Link to comment Share on other sites More sharing options...
Rila_xp Posted July 19, 2012 Author Report Share Posted July 19, 2012 este INT Quote Link to comment Share on other sites More sharing options...
titus1 Posted July 19, 2012 Report Share Posted July 19, 2012 Scapa de limita si de order, nu asa functioneaza sum. Pune conditia de limitare in where. Quote Link to comment Share on other sites More sharing options...
Cril Posted July 19, 2012 Report Share Posted July 19, 2012 sau poti face asa :$query = mysql_query("SELECT vizite FROM statistics ORDER BY date DESC LIMIT 0,4");// Print out resultwhile($rowz = mysql_fetch_array($query)){ $sumz = $rowz['vizite']; $suma = $sumz + $suma;}echo "This week $suma <br>";?> Quote Link to comment Share on other sites More sharing options...
Rila_xp Posted July 21, 2012 Author Report Share Posted July 21, 2012 Am rezolvat asa:$query=mysql_query("SELECT sum(vizite) FROM (SELECT vizite FROM statistics ORDER BY date DESC LIMIT 4) AS subquery", $con);while($row = mysql_fetch_array($query)){ $sum = $row['sum(vizite)'];} Quote Link to comment Share on other sites More sharing options...