在WHILE / Loop内运行查询

时间:2011-10-27 02:40:53

标签: php mysql sql while-loop

在这里需要帮助,得到一个查询,计算每个成员的行数并通过电子邮件发送给他们。

所以现在他们收到一封简单的电子邮件,告诉他们有多少帖子,我搜索了一种在主查询中运行第二个查询的方法(从邮件表中计算帖子,根据他们的邮件生成电子邮件)另一个表中的帐户信息)我们想要的是让电子邮件显示每个帖子,我已经在第一个查询中加入表格,我似乎无法找到在while语句中运行第二个的方法。

以下是我们目前的代码。任何帮助将不胜感激。

<?php

//Connect to mysql server
$link = mysql_connect(localhost, xxxxxxx, xxxxxxxx);
if(!$link) {
    die('Failed to connect to server: ' . mysql_error());
}

//Select database
$db = mysql_select_db(systm_test);
if(!$db) {
    die("Unable to select database");
}
$count = 0; 
$date = date("Y-m-d", $_SERVER['REQUEST_TIME']);
$day = date("l F jS, Y", $_SERVER['REQUEST_TIME']);

$qry= sprintf("SELECT p.tech, p.date, e.email, e.fullname, "
            . "COUNT( * ) AS record_count "
            . "FROM users_test AS e "
            . "JOIN sc AS p ON e.tech = p.tech "
            . "WHERE DATE = '%s' "
            . "GROUP BY p.tech, e.fullname, e.email, p.date",
          $date
      );

$result = mysql_query($qry); 

if (!$result) { 
    # something went wrong... 
    error_log(sprintf('%d: %s', mysql_errno(), mysql_error())); 
    echo 'Unable to retrieve callpost report';

} else { 

    while ($node = mysql_fetch_object($result)) { 



        $headers = "From: xxxxxxxxxx <noreply@xxxxx.xxx>\r\n"; 
        $headers .= "Content-Type: text/html; charset=\"iso-8859-1\"\r\n"; 
        $headers .= "X-Priority: 1 (Higuest)\r\n"; 
        $headers .= "Importance: High\r\n"; 

        $subject = sprintf('%s | Post Report', $node->fullname); 

            $date = date("Y-m-d", $_SERVER['REQUEST_TIME']);


        $body = sprintf( 
            'Dear %s, <br><br> 
            Your daily post report for %s. 
            <br> 
            <br>     
            System Info<br> 
            ------------------------------------<br> 
            Member Id:      <b>%d</b><br> 
            Posts:      <b>%d</b>
            <br>
            System Name:    TEST<br>', 

            $node->fullname, 
            $day, 
            $node->tech, 
            $node->record_count 
        ); 

        if (!mail($node->email, $subject, $body, $headers)) 
        { 
            error_log( 
                sprintf('failed sending email report to %s (%d)', 
                    $node->fullname, 
                    $node->tech) 
            ); 
        } 
    } //end while statement 
     mail($node->email, $subject, $body, $headers);
  }
}
?>

1 个答案:

答案 0 :(得分:0)

您要查找的是子选择。像。的东西。

"SELECT p.tech, p.date, e.email, e.fullname, "
        . "COUNT( * ) AS record_count, "
        . "(SELECT COUNT(*) FROM Posts z WHERE z.author = e.tech) as post_count "
        . "FROM users_test AS e "
        . "JOIN sc AS p ON e.tech = p.tech "
        . "WHERE DATE = '%s' "
        . "GROUP BY p.tech, e.fullname, e.email, p.date"