JJ___
JJ___

Reputation: 207

Wrong output of after I sum the data from database

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?

Upvotes: 0

Views: 86

Answers (4)

Kalpit
Kalpit

Reputation: 4936

$query=mysql_fetch_array($Annual);

echo $query[0];

it will print what you want exactly...

Upvotes: 0

Lwyrn
Lwyrn

Reputation: 1911

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>

Upvotes: 1

MarkTWebsite
MarkTWebsite

Reputation: 21

You should add;

$annual2 = mysql_fetch_array($annual);

then

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

Hope this helps :)

Upvotes: 0

Gerben Jacobs
Gerben Jacobs

Reputation: 4583

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)

Upvotes: 0

Related Questions