Query results by user
Posted: Wed Nov 03, 2010 5:15 am
Hi,
I'm looking for some help with a problem which has caused much confusion.
To give some background, I'm converting a timesheet program from MS Access to PHP/MySql. This is my first attempt with PHP/MySql and have become somewhat stumped with what is an easy process in Access.
What I'm trying to do...
Sum the hours worked in a week by the user logging the work and show this on a page with a summary of what they have done.
The problem I've got...
I don't quite understand how to put the relevant session variables into the Where statement of the query - the username and relevant week number are both session variables called and displayed on the page.
The code I'm using....
<?php $sql = "SELECT HoursSpentLas, TravelTimeLas FROM tblProjects WHERE AdviserName = $MM_Username AND WeekNumber = $MM_Weeknumber";
$query = mysql_query($sql);
while($row = mysql_fetch_assoc($query)){
echo $query['HoursSpentLas'];
}
$TimeWorkedLAs = "SELECT HoursSpentLas, SUM(HoursSpentLas) FROM tblProjects GROUP BY WeekNumber";
$result = mysql_query($TimeWorkedLAs) or die(mysql_error());
// Print out result
while($row = mysql_fetch_array($result)){
echo "Total = ". $row['SUM(HoursSpentLas)'];
}
?>
This code does the addition fine but is returning values for all of the users (i get Total = 20 Total = 36 Total = 24 etc) . I've tried allsorts of variations but just can't figure it out.
Any help gratefully received!
Many thanks
Dave
I'm looking for some help with a problem which has caused much confusion.
To give some background, I'm converting a timesheet program from MS Access to PHP/MySql. This is my first attempt with PHP/MySql and have become somewhat stumped with what is an easy process in Access.
What I'm trying to do...
Sum the hours worked in a week by the user logging the work and show this on a page with a summary of what they have done.
The problem I've got...
I don't quite understand how to put the relevant session variables into the Where statement of the query - the username and relevant week number are both session variables called and displayed on the page.
The code I'm using....
<?php $sql = "SELECT HoursSpentLas, TravelTimeLas FROM tblProjects WHERE AdviserName = $MM_Username AND WeekNumber = $MM_Weeknumber";
$query = mysql_query($sql);
while($row = mysql_fetch_assoc($query)){
echo $query['HoursSpentLas'];
}
$TimeWorkedLAs = "SELECT HoursSpentLas, SUM(HoursSpentLas) FROM tblProjects GROUP BY WeekNumber";
$result = mysql_query($TimeWorkedLAs) or die(mysql_error());
// Print out result
while($row = mysql_fetch_array($result)){
echo "Total = ". $row['SUM(HoursSpentLas)'];
}
?>
This code does the addition fine but is returning values for all of the users (i get Total = 20 Total = 36 Total = 24 etc) . I've tried allsorts of variations but just can't figure it out.
Any help gratefully received!
Many thanks
Dave