开发者

php loop in mysql query

开发者 https://www.devze.com 2023-03-20 09:11 出处:网络
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`

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,

Chand开发者_如何学运维ru.


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消