首页 > 解决方案 > 如何将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));
        }
        ?>

标签: phphtmlvariablesecho

解决方案


使用连接.运算符,像这样

 $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;

推荐阅读