0

Let say I wan to sum up the Duration in my leave table and with condition Leave_Type = 'Annual' and Status = 'Approved'

My database

table leave
{Leave_ID(PK), Leave_Type, Status, Duration, Emp_ID(FK)}

table employee
{Emp_ID(PK), Emp_Name}

I use the following code to sum the Duration

$aid=$_SESSION["eid"];

$Annual = mysql_query("select SUM(Duration)

FROM `leave`

WHERE leave.Leave_Type = 'Annual' and leave.Status = 'Approved' 
and leave.Emp_ID = employee.Emp_ID and leave.Emp_ID = $aid");

The way i display the output

<td><?php echo $Annual;?></td>

But the output I get is something like "Resourse id#4". That's not the output I want and it should be the total sum of duration. Is there is any problem with mysql_query or the way i print my output?

3
  • What is the type of the duration column? Integer, Datetime? Commented Sep 2, 2013 at 9:03
  • Your query filters by employee.Emp_ID but doesn't read employee. It's impossible that it returns any output: it must be crashing. Commented Sep 2, 2013 at 9:13
  • Actually, I think it will work just fine Commented Sep 2, 2013 at 9:16

4 Answers 4

1

You are doing wrong the output!

$AnnualData = mysql_fetch_assoc($Annual);

Than the output:

<td><?php echo $AnnualData['SUM(Duration)'];?></td>

If you want you can add an alias to the sum statement like this ( in the sql query ):

SUM(Duration) AS total

and than output like this:

<td><?php echo $AnnualData['total'];?></td>
Sign up to request clarification or add additional context in comments.

1 Comment

Better yet, put a name on the field. SUM(Duration) as leaveDuration and retrieve it as $AnnualData['leaveDuration']
0

You're only running the query, you need to fetch the data.

Change mysql_query to mysqli_query (the former one is deprecated) and then use mysqli_fetch_assoc($Annual)

Comments

0

You should add;

$annual2 = mysql_fetch_array($annual);

then

<?PHP echo $annual2['Duration']; ?>

Hope this helps :)

3 Comments

This will not work as you are selected the SUM of Duration and not Duration itself
Okay then, add $annual3 = SUM($annual2['duration'];
I'm sorry but no, look at the query and the answer Lwyrn gave earlier.
0
$query=mysql_fetch_array($Annual);

echo $query[0];

it will print what you want exactly...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.