我试图将一个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
更多推荐
发布评论