MySQL结果在PHPMailer电子邮件的正文中(MySQL Results in Body of PHPMailer Email)

编程入门 行业动态 更新时间:2024-10-27 00:24:10
MySQL结果在PHPMailer电子邮件的正文中(MySQL Results in Body of PHPMailer Email)

我试图将一个MySQL查询的结果回传到通过PHPMailer发送的电子邮件正文中,但遇到了困难。 我在页面上成功创建表时查询工作正常,但似乎无法将表分配给变量正确。

我的代码:

$body = '<html> <body> <table> <thead> <tr> <th>Food</th> <th>Quantity</th> <th>Category</th> <tr> </thead> <tbody>'. while($row = $resultOrderE->fetch(PDO::FETCH_ASSOC)){ echo ' <tr> <td>$row['food']</td> <td>$row['quantity']</td> <td>$row['category']</td> </tr> ';}.' </tbody> </table> </body> </html>';

我得到的错误是:

PHP Parse error: syntax error, unexpected 'while' (T_WHILE)

有什么建议么? 谢谢!

I'm trying to echo the results of a MySQL query into the body of an email to send via PHPMailer, but am having difficulties. The query works as I successfully create the table on the page, but can't seem to get the assigning the table to a variable correct.

My Code:

$body = '<html> <body> <table> <thead> <tr> <th>Food</th> <th>Quantity</th> <th>Category</th> <tr> </thead> <tbody>'. while($row = $resultOrderE->fetch(PDO::FETCH_ASSOC)){ echo ' <tr> <td>$row['food']</td> <td>$row['quantity']</td> <td>$row['category']</td> </tr> ';}.' </tbody> </table> </body> </html>';

The error I get is:

PHP Parse error: syntax error, unexpected 'while' (T_WHILE)

Any suggestions? Thanks!

最满意答案

你不能将WHILE循环连接到一个字符串。

你必须循环遍历并在每次循环迭代时添加到现有字符串

$html_string = '<html><body><table><thead><tr><th>Food</th><th>Quantity</th> <th>Category</th><tr></thead><tbody>'; while($row = $resultOrderE->fetch(PDO::FETCH_ASSOC)){ $html_string .= '<tr><td>'.$row['food'].'</td><td>'.$row['quantity'].'</td><td>'.$row['category'].'</td></tr>'; } // this will add the closing tags and now html_string has your built email $html_string .= '</tbody></table></body></html>';

所以.=是重要的部分,它将字符串连接到现有字符串的末尾

you cant concate a WHILE loop to a string.

you have to loop through and ADD to the existing string per iteration of the loop

$html_string = '<html><body><table><thead><tr><th>Food</th><th>Quantity</th> <th>Category</th><tr></thead><tbody>'; while($row = $resultOrderE->fetch(PDO::FETCH_ASSOC)){ $html_string .= '<tr><td>'.$row['food'].'</td><td>'.$row['quantity'].'</td><td>'.$row['category'].'</td></tr>'; } // this will add the closing tags and now html_string has your built email $html_string .= '</tbody></table></body></html>';

so the .= is the important part, it concates a string to the end of existing string

更多推荐

本文发布于:2023-08-07 03:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1459540.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文中   电子邮件   PHPMailer   MySQL   Body

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!