在 WHILE/循环内运行查询

发布于 2024-12-11 20:59:31 字数 2646 浏览 0 评论 0原文

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

所以现在他们收到一封简单的电子邮件,告诉他们有多少帖子,我已经找到了一种在主查询中运行第二个查询的方法(它计算帖子表中的帖子,根据他们的帐户信息生成电子邮件)另一个表)我们想要的是让电子邮件也显示每个帖子,我已经在第一个查询中加入表,但我似乎找不到在 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 <[email protected]>\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);
  }
}
?>

Need help here, got a query that counts number of rows for each member and emails it to them.

So right now they get a simple email telling them how many posts they have, I have searched for a way to run a second query inside the main query (which counts the posts from the post table, generates the email based on their account info in another table) What we want is to have the email show each post as well, I am already joining tables in my first query and I can't seem to find a way to run a second inside the while statement.

Here is the code we currently have. Any help would be greatly appreciated.

<?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 <[email protected]>\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);
  }
}
?>

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

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

发布评论

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

评论(1

小鸟爱天空丶 2024-12-18 20:59:31

子选择就是您所寻找的。类似的东西。

"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"

A sub-select is what your looking for. Something like.

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