简体   繁体   English

MySQL在PHPMailer电子邮件主体中的结果

[英]MySQL Results in Body of PHPMailer Email

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. 我试图将MySQL查询的结果回显到通过PHPMailer发送的电子邮件正文中,但是遇到了困难。 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! 谢谢!

you cant concate a WHILE loop to a string. 您不能将WHILE循环连接到字符串。

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 所以.=是很重要的部分,它将一个字符串包含在现有字符串的末尾

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM