0

I have a big mysql query like this:

SELECT u.userid AS `User`
  , SUM(CASE WHEN activitydate='2011-07-01' THEN round(time/60) ELSE 0 END) AS `2011-07-01`
  , SUM(CASE WHEN activitydate='2011-07-02' THEN round(time/60) ELSE 0 END) AS `2011-07-02`
.......
  , SUM(CASE WHEN activitydate='2011-07-30' THEN round(time/60) ELSE 0 END) AS `2011-07-30`
FROM hoursbase h
  JOIN person u
    ON h.userid = u.id
WHERE h.activitydate BETWEEN '2011-07-01' AND '2011-07-30'
GROUP BY h.userid
ORDER BY h.userid

Is there any way that i can put above query in loop using php.

Also i am try to add one drop down menu and on selecting, the respective month will update in query.

Regards,
Chandru.

5
  • If the query is working in MySQL, why move it into PHP? What is the problem you are trying to solve? Commented Jul 14, 2011 at 6:39
  • you can group the date in some variable, in loop increment the date value and concate it with your query Commented Jul 14, 2011 at 6:47
  • don't do that: looping query in mysql is much faster than doing the same in php Commented Jul 14, 2011 at 7:01
  • @ k102 no i dint mean to loop query. i just meant to concate the looped date variable to query Commented Jul 14, 2011 at 7:05
  • I am using above query to genarate report. For a perticular month i need to write query of 30 lines (one line for each day). Also i need to showcase report to my team members, each time i can't open mysql and show.. Commented Jul 14, 2011 at 7:12

1 Answer 1

3

If you don't mind the query having a different output format you can rewrite it like so:

SELECT u.userid AS `User`
   ,activitydate
   ,sum(round(ifnull(time,0)/60)) as timetaken
FROM hoursbase h
JOIN person u ON h.userid = u.id 
WHERE h.activitydate BETWEEN :startdate AND :enddate  /*note the : params*/ 
GROUP BY h.userid, h.activitydate 
ORDER BY h.userid, h.activitydate

This will return your data grouped by userid first and then by activitydate.
It will also run a lot faster.
Finally it will be easier to get the results per user per date in php.
And when you change months you don't have to change the number of columns.

Here's how to loop through it in php using a loop:
I've copied the code from this answer: How do I loop through a MySQL query via PDO in PHP?

// $attrs is optional, this demonstrates using persistent connections, 
// the equivalent of mysql_pconnect 
$attrs = array(PDO::ATTR_PERSISTENT => true);  

// connect to PDO 
$pdo = new PDO("mysql:host=localhost;dbname=test", "user", "password", $attrs);
// the following tells PDO we want it to throw Exceptions for every error. 
// this is far more useful than the default mode of throwing php errors 
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);  
// prepare the statement. the place holders allow PDO to handle substituting 
// the values, which also prevents SQL injection 
$stmt = $pdo->prepare("SELECT u.userid AS `User`.....  ");  
// bind the parameters 
$stmt->bindValue(":startdate", "2011-07-01"); 
$stmt->bindValue(":enddate", "2011-07-31");  
// initialise an array for the results  
$products = array(); 
if ($stmt->execute()) {   
  while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {      
    //do more useful stuff here
    //escape all fields that can be entered by users using htmlspecialchars
    //to prevent XSS exploits.
    echo htmlspecialchars($row['User']);
    echo htmlspecialchars($row['activitydate']);
    echo $row['timetaken'];
  } 
}  
// set PDO to null in order to close the connection 
$pdo = null; 

About htmlspecialchars()
You need to escape all string fields that can be entered by a user and that you output to screen.
In this case I escaped userid and activitydate because I'm only 95% sure these are integer and date fields, I'd skip escaping if I was 100% sure, but if I'm not I have to escape.

Links:
How to escape output in PHP
How do I loop through a MySQL query via PDO in PHP?

Sign up to request clarification or add additional context in comments.

10 Comments

Hi thanks for your code. I tried to execute the above code and i got below error "Notice: Undefined index: User in C:\wamp\www\chandru\month.php on line 25" for the line "echo htmlspecialchars($output['User']);"
Hey Johan your are awesome.. Thanks for modification... At present i am getting output, but it is not meeting my expectations. I am looking the output as mentioned in the question "stackoverflow.com/questions/6676653/…". I think you can help me..
@Chandru, you'll need to add code to put the echo output in a table layout using two loops, the inner one for the columns, the outer one for the rows. I don't have time to help you with that now, try and see what you can come up with, when it sort of works I suggest asking a new question about that so people can help you perfecting it.
thanks for your precious time.. I need minor tweak in the code, at present i am getting only the dates where timetaken is having value. It there way i can get all the dates and zero can be printed where timetaken value is blank.
@Ricardo That's not how it's done. The input gets stored in the database as the user has entered it. (minus SQL-injection issues). Only when outputting the data do you escape the data to prevent XSS issues. Otherwise your database will get corrupted with all kinds of XSS escaping data which may or may not be relevant (perhaps the output goes to a printer as well?) Besides if the XSS-escaping code gets updated my way of doing things will include the updated code; your way of doing things will leave the (newly found) exploit in place.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.