Jump to content

Recommended Posts

Hey

I 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 - 1
id 2 - goals - 1
id 3 - goals - 1
id 4 - goals - 1

How do i plus this four 1 numbers together so the result will be 4?
Link to comment
https://forums.phpfreaks.com/topic/32719-solved-simpel-quesstion-i-think/
Share on other sites

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.
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 38

Here is the code im using:

[code]
<?php
include('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]
ahh... where's my head?  I meant SUM, but you're using it incorrectly:
[code]<?php
include('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]
No, you use SUM inthe query
[code]<?php
include('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]
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]
<?php
include('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 :)
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]
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.