php - 如何将while循环数据存储在字符串变量中,然后在php的邮件函数中使用它
问题描述
现在,它不会在邮件中打印任何内容,我知道我必须将循环输出存储到某个字符串变量,然后将其作为 $message 传递给邮件函数。
然后,此代码将创建为 php 函数,并用于不同的邮件收件人发送邮件。
<?php
include db.php;
$b ="select cnt,Bookings from table";
$result=mysqli_query($dbconn,$b);
echo "<table border='1'>
<tr style= 'background-color:blue'>
<th style= 'color:white'>cnt</th>
<th style= 'color:white'>Bookings</th>
</tr>";
while (mysqli_more_results($dbconn)) {
if ($result = mysqli_store_result($dbconn)) {
while ($row = mysqli_fetch_assoc($result)) {
echo "\n<tr>";
echo "\n<td>" . $row["cnt"] . "\n</td>";
echo "\n<td>" . $row["Bookings"] . "\n</td>";
echo "\n</tr>";
}
mysqli_free_result($result);
}
mysqli_next_result($dbconn);
}
echo "</table>";
$subject = '';
$message = $result;
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-type: text/html; charset=iso-8859-1';
mail($to, $subject, $message, implode("\r\n", $headers));
}
?>
解决方案
使用连接.
运算符,像这样
$html = '<!doctype html>
<html>
<head></head>
<body>
<table border="1">
<tr style= "background-color:blue">
<th style= "color:white">cnt</th>
<th style= "color:white">Bookings</th>
</tr> ';
$b ="select cnt,Bookings from table";
$result=mysqli_query($dbconn,$b);
while (mysqli_more_results($dbconn)) {
if ($result = mysqli_store_result($dbconn)) {
while ($row = mysqli_fetch_assoc($result)) {
$html .= '<tr>';
$html .= '<td>'. $row["cnt"] .'</td>';
$html .= '<td>'. $row["Bookings"] . '</td>';
$html .= '</tr>';
}
mysqli_free_result($result);
}
mysqli_next_result($dbconn);
}
$html .='</table> </body></html>';
$message = $html;
推荐阅读
- python - 将 ARIMAX 预测分解为 exog 变量和 ARIMA 参数的贡献
- android - 如何检测 Jetpack Compose 中变换手势的结束?
- node.js - Node Express - PayloadTooLargeError:请求实体太大
- java - 为什么记录视图和记录按钮会抛出异常?(不是任何其他问题的重复,也没有找到解决方案)
- python-3.x - 将数据框的一列压缩到基于 Python3 中具有相同值的另一列的列表
- python - 关于win10toast和win10toast-click,通知中心有问题
- pine-script - 如何将 TradingView 上的复杂指标转换为 MT4 的语言?
- .net - System.Text.Json 如何反序列化
- python - Dask:真正懒惰的排序
- javascript - React useQuery 钩子一直在组件内运行