Justafriend Posted September 1, 2014 Share Posted September 1, 2014 Ok i have 3 columms right now i have date player and points and what i want is for this script to add up each players points and send it to page as totals decending i have got the page to show them individually but when i do group it only gets one record so i tried to add in the sum points and it gives me a ) Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in so i am kind of lost as to where adding the sum(points) would cause this below is the code where the problem is any help would be much appreciated $r = mysql_query("select * from player_points Sum(points) as total where date_entered > '$d' Group by player order by total DESC"); Quote Link to comment https://forums.phpfreaks.com/topic/290788-adding-points/ Share on other sites More sharing options...
jcbones Posted September 2, 2014 Share Posted September 2, 2014 $r = mysql_query("select sum(points) as total from player_points where date_entered > '$d' Group by player order by total DESC") or trigger_error(mysql_error()); *Note*, You shouldn't be using mysql, it is ver` old, and a `neer do well. Stick with PDO, or Mysqli. Quote Link to comment https://forums.phpfreaks.com/topic/290788-adding-points/#findComment-1489579 Share on other sites More sharing options...
Justafriend Posted September 2, 2014 Author Share Posted September 2, 2014 (edited) i am right now in a rush to get this little project done eventually im gonna start looking at mysqli but all the code that i have is mysql so i got to tweak it then get it running then switch and learn about mysqli i just tried the solution you gave me but came across a weird output The numbered column for their place is all there but the names and points arent showing ill just paste the top 10 and then also paste the source that the page shows up Extracting data since 2014-09-01 Place PlayerName Points 1 2 3 4 5 6 7 8 9 10 and the source ends up <tr><td>2</td><td></td><td></td></tr> <tr><td>3</td><td></td><td></td></tr> <tr><td>4</td><td></td><td></td></tr> <tr><td>5</td><td></td><td></td></tr> <tr><td>6</td><td></td><td></td></tr> <tr><td>7</td><td></td><td></td></tr> <tr><td>8</td><td></td><td></td></tr> so its not outputting the names or points to the table i really appreciate yourr help Edited September 2, 2014 by Justafriend Quote Link to comment https://forums.phpfreaks.com/topic/290788-adding-points/#findComment-1489580 Share on other sites More sharing options...
QuickOldCar Posted September 2, 2014 Share Posted September 2, 2014 paste your code here in the code tags for more assistance Quote Link to comment https://forums.phpfreaks.com/topic/290788-adding-points/#findComment-1489581 Share on other sites More sharing options...
Justafriend Posted September 2, 2014 Author Share Posted September 2, 2014 here it is i know it isnt the best but im trying to merge the code someone gave me and work with it as i learn <table> <thead> <td>Place</td> <td>PlayerName</td> <td>Points</td> </thead> <?php mysql_connect("localhost", "root", ""); //Put login and password for mysql here. Replace null with mysql server address if provided by host. mysql_select_db("test") or die(mysql_error());//Put databasename here $i = 0; $d = date("2014-09-01"); //$d = preg_replace("/-(\d+) /", "-01 ", $d); echo("Extracting data since $d<br/>"); $r = mysql_query("select sum(Points) as total from player_points where date_entered > '$d' Group by player order by total DESC") or trigger_error(mysql_error()); while ($v = mysql_fetch_assoc($r)) { ++$i; echo("<tr><td>$i</td><td>${v["player"]}</td><td>${v["points"]}</td></tr>\n"); } ?> </table> Quote Link to comment https://forums.phpfreaks.com/topic/290788-adding-points/#findComment-1489582 Share on other sites More sharing options...
QuickOldCar Posted September 2, 2014 Share Posted September 2, 2014 Not fetching player from the table so it won't show as $v['player'] in the results You are returning the sum only Use a subquery, * is all...you can return only what you need $r = mysql_query("select *,(select sum(Points) as total ) from player_points where date_entered > '$d' Group by player order by total DESC") or trigger_error(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/290788-adding-points/#findComment-1489585 Share on other sites More sharing options...
QuickOldCar Posted September 2, 2014 Share Posted September 2, 2014 This is easier echo "<tr><td>".$i."</td><td>".$v['player']."</td><td>".$v['points']."</td></tr><br />"; Quote Link to comment https://forums.phpfreaks.com/topic/290788-adding-points/#findComment-1489586 Share on other sites More sharing options...
Justafriend Posted September 2, 2014 Author Share Posted September 2, 2014 after i substituted that for what i had i got 2 errors one looks almost french which is weird and that is Notice: Champ 'total' inconnu dans order clause in C:\wamp\www\points.php on line 29 and the second is Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\wamp\www\points.php on line 30 here is the revised code <table> <thead> <td>Place</td> <td>PlayerName</td> <td>Points</td> </thead> <?php mysql_connect("localhost", "root", ""); //Put login and password for mysql here. Replace null with mysql server address if provided by host. mysql_select_db("test") or die(mysql_error());//Put databasename here $i = 0; $d = date("2014-09-01"); //$d = preg_replace("/-(\d+) /", "-01 ", $d); echo("Extracting data since $d<br/>"); $r = mysql_query("select *,(select sum(Points) as total ) from player_points where date_entered > '$d' Group by player order by total DESC") or trigger_error(mysql_error()); while ($v = mysql_fetch_assoc($r)) { ++$i; echo("<tr><td>$i</td><td>${v["player"]}</td><td>${v["points"]}</td></tr>\n"); } ?> </table> Quote Link to comment https://forums.phpfreaks.com/topic/290788-adding-points/#findComment-1489587 Share on other sites More sharing options...
jcbones Posted September 2, 2014 Share Posted September 2, 2014 (edited) You should be able to pull the name without a sub-query. "select player, sum(Points) as total from player_points where date_entered > '$d' Group by player order by total DESC" Edited September 2, 2014 by jcbones Quote Link to comment https://forums.phpfreaks.com/topic/290788-adding-points/#findComment-1489589 Share on other sites More sharing options...
jcbones Posted September 2, 2014 Share Posted September 2, 2014 You should also be looking for: $v['player']; $v['total']; according to your query. Quote Link to comment https://forums.phpfreaks.com/topic/290788-adding-points/#findComment-1489590 Share on other sites More sharing options...
Justafriend Posted September 2, 2014 Author Share Posted September 2, 2014 (edited) Thank you very much both of you now one more question and you can tell me forget it i really do appreciate the help you have given me is there anyway of telling the numbering that if using an example to show 1 sajoma 58 2 Atlfan 55 3 BikerChick 47 4 PRO_milagros 45 5 Rustedfish 41 6 TM7_LOVIN_LIFE_ 40 7 A_Baddboy 35 8 I_DontPlay 34 9 EU_IT_mirejo 32 10 J1moyer 32 11 mokan40 30 12 BigCube 30 that instead of a result like this it would be like this cause 9 and 10 are tied as well as 11 and 12 so the resulting would look like 1 sajoma 58 2 Atlfan 55 3 BikerChick 47 4 PRO_milagros 45 5 Rustedfish 41 6 TM7_LOVIN_LIFE_ 40 7 A_Baddboy 35 8 I_DontPlay 34 9 EU_IT_mirejo 32 9 J1moyer 32 11 mokan40 30 11 BigCube30 if it cant be done its not a big deal Edited September 2, 2014 by Justafriend Quote Link to comment https://forums.phpfreaks.com/topic/290788-adding-points/#findComment-1489594 Share on other sites More sharing options...
Solution Barand Posted September 2, 2014 Solution Share Posted September 2, 2014 It can be done, but things get a little more complicated and require user variables and sub-queries SELECT @rank := IF(tot=@prevtot, @rank, @rank+1) as rank , player , @prevtot := tot as total FROM (SELECT player , SUM(points) as tot FROM player_points JOIN (SELECT @rank:=0, @prevtot:=0) as init GROUP BY player ORDER BY tot DESC ) as tots ORDER BY rank; Quote Link to comment https://forums.phpfreaks.com/topic/290788-adding-points/#findComment-1489606 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.