我正在使用这段代码将数据从 mysql 发送到电子邮件中:
$sql = "SELECT * FROM orders ORDER BY id";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0)
{
// append data of each row to $msg.
while($row = mysqli_fetch_assoc($result))
{
$body .= " <style>
table{width:100%}
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 5px;
text-align: left;
}
</style> ".
"<table>".
"<tr> <th> name </th>
<th>last name</th>
<th>email</th>
</tr>".
"<tr>".
"<td>". $row["col1"]. "</td>".
"<td>" . $row["col2"]. "</td>".
"<td>" . $row["col3"]. "</td>".
"</tr>".
"</table>" ;
}
$body = wordwrap($body,70);
mail($to_email, $subject, $body, $headers);
}
else
{
echo "0 results";
}
mysqli_close($conn);
?>
它工作正常,但唯一的问题是我得到了多个表,但我想在单个表中显示所有行。
谁能告诉我我做错了什么或如何解决?
最佳答案
将表格 HTML 标记移到循环之外:
<table>
<thead>
<th>Header</th>
</thead>
<tbody>
<?php //put your loop here ?>
</tbody>
</table>
关于php - 从 mysql 中获取数据到电子邮件中的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58730250/