在 WHILE/循环内运行查询
这里需要帮助,得到一个查询,该查询计算每个成员的行数并将其通过电子邮件发送给他们。
所以现在他们收到一封简单的电子邮件,告诉他们有多少帖子,我已经找到了一种在主查询中运行第二个查询的方法(它计算帖子表中的帖子,根据他们的帐户信息生成电子邮件)另一个表)我们想要的是让电子邮件也显示每个帖子,我已经在第一个查询中加入表,但我似乎找不到在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
子选择就是您所寻找的。类似的东西。
A sub-select is what your looking for. Something like.