JJohnsenDK Posted January 3, 2007 Share Posted January 3, 2007 HeyI need to know how i can count my database:Example:In my database i have a colum named goals in this colum i have four 1 numbers in each field like this:id 1 - goals - 1id 2 - goals - 1id 3 - goals - 1id 4 - goals - 1How do i plus this four 1 numbers together so the result will be 4? Quote Link to comment Share on other sites More sharing options...
ober Posted January 3, 2007 Share Posted January 3, 2007 SELECT COUNT(goal_column) as goal_count FROM tablexyz Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 3, 2007 Share Posted January 3, 2007 Your question is a little vague. If you just want the number of records then use COUNT()However if you want the total number of goals then you would use SUM().If you are wanting the total count or sum of records where the goals is equal to 1, then you would need to add a WHERE clause. Quote Link to comment Share on other sites More sharing options...
JJohnsenDK Posted January 3, 2007 Author Share Posted January 3, 2007 I want the total number of goals and i tried SUM(), but i dont think that function exists because i get this error:Fatal error: Call to undefined function SUM() in C:\Programmer\xampp\xampp\htdocs\test\test\pl_details2.php on line 38Here is the code im using:[code]<?phpinclude('config.php');$sql = "SELECT actions.players_ID, actions.goal, actions.game_ID, player.fname, player.lname FROM actions INNER JOIN player ON player.player_id = ".$_GET['id'].""; $result = mysql_query($sql);$result2 = mysql_query($sql);$row = mysql_fetch_array($result);?> <table> <tr> <td>Navn:</td> <td><?=$row['fname']." ".$row['lname']; ?></td> </tr> <tr> <td>Kampe:</td> <td><?=$row['matchs']; ?></td> </tr> <tr> <td>MĆ„l:</td> <td> <?php while($row2 = mysql_fetch_array($result2)){ if(!empty($row2['goal'])){ $coun = SUM($row2['goal']); echo $coun; } } ?> </td> </tr></table>[/code] Quote Link to comment Share on other sites More sharing options...
ober Posted January 3, 2007 Share Posted January 3, 2007 ahh... where's my head? I meant SUM, but you're using it incorrectly:[code]<?phpinclude('config.php');$sql = "SELECT actions.players_ID, SUM(actions.goal) as goalcount, actions.game_ID, player.fname, player.lname FROM actions INNER JOIN player ON player.player_id = ".$_GET['id'].""; $result = mysql_query($sql);$result2 = mysql_query($sql);$row = mysql_fetch_array($result);?> <table> <tr> <td>Navn:</td> <td><?=$row['fname']." ".$row['lname']; ?></td> </tr> <tr> <td>Kampe:</td> <td><?=$row['matchs']; ?></td> </tr> <tr> <td>MĆ„l:</td> <td><?=echo $row['goalcount'])?></td> </tr></table>[/code] Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 3, 2007 Share Posted January 3, 2007 No, you use SUM inthe query[code]<?phpinclude('config.php');$sql = "SELECT a.players_ID, SUM(a.goal) as goals, a.game_ID, p.fname, p.lname FROM actions a INNER JOIN player p ON p.player_id = a.players_ID WHERE a.player_id = " . $_GET['id']; $result = mysql_query($sql);$result2 = mysql_query($sql);$row = mysql_fetch_array($result);?> <table> <tr> <td>Navn:</td> <td><?=$row['fname']." ".$row['lname']; ?></td> </tr> <tr> <td>Kampe:</td> <td><?=$row['matchs']; ?></td> </tr> <tr> <td>MĆ„l:</td> <td>?=$row['goals']; ?></td> </tr></table>[/code] Quote Link to comment Share on other sites More sharing options...
JJohnsenDK Posted January 3, 2007 Author Share Posted January 3, 2007 I tried both of your solutions and none of them work. I know you just wrote the query differently but it does not work. Here is the error that i get:[i]Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Programmer\xampp\xampp\htdocs\test\test\pl_details2.php on line 20[/i]And the code im using:[code]<?phpinclude('config.php');$sql = "SELECT actions.players_ID, SUM(actions.goal) AS goals, actions.game_ID, player.fname, player.lname FROM actions INNER JOIN player ON player.player_id = ".$_GET['id'].""; $result = mysql_query($sql);$result2 = mysql_query($sql);$row = mysql_fetch_array($result);?> <table> <tr> <td>Navn:</td> <td><?=$row['fname']." ".$row['lname']; ?></td> </tr> <tr> <td>Kampe:</td> <td><?=$row['matchs']; ?></td> </tr> <tr> <td>MĆ„l:</td> <td> <?=$row['goals']; ?> </td> </tr></table>[/code]So i dont i understand why i get this error? I dont get the error if i take out the SUM() function, so its something to do with that :) Quote Link to comment Share on other sites More sharing options...
Psycho Posted January 3, 2007 Share Posted January 3, 2007 Change this:[code]$result = mysql_query($sql);$result2 = mysql_query($sql);[/code]To this and tell us what is output to the page[code]$result = mysql_query($sql) or die("The query:<br>".$sql."<br>caused the following error:<br>".mysql_error());[/code] Quote Link to comment Share on other sites More sharing options...
JJohnsenDK Posted January 3, 2007 Author Share Posted January 3, 2007 aaah yes i forgot the debug... after that i could find the error myself... thanks :) 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.