mysql查询中的php循环

发布于 2024-11-19 17:51:15 字数 607 浏览 0 评论 0原文

我有一个像这样的大mysql查询:

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

有什么方法可以使用php将上面的查询放入循环中吗?

另外,我尝试添加一个下拉菜单,选择后,相应的月份将在查询中更新。

问候,
钱德鲁。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧情别恋 2024-11-26 17:51:15

如果您不介意查询具有不同的输出格式,您可以像这样重写它:

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

这将首先返回按 userid 分组的数据,然后按 activitydate 分组。
它还会运行得快得多
最后,在 php 中获取每个用户每个日期的结果会更容易。
当您更改月份时,您不必更改列数。

以下是如何在 php 中使用循环来循环它:
我从这个答案复制了代码: 如何在 PHP 中通过 PDO 循环执行 MySQL 查询?

// $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; 

关于 htmlspecialchars()
您需要转义用户可以输入并输出到屏幕的所有字符串字段。
在这种情况下,我转义了 useridactivitydate 因为我只有 95% 确定这些是整数和日期字段,如果我 100% 确定,我会跳过转义,但是如果我不是,我就必须逃跑。

链接:
如何转义 PHP 中的输出
如何循环执行 MySQL 查询通过 PHP 中的 PDO?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文