首页 > 解决方案 > 如何将所有 $row 变量写入文本文件

问题描述

我有以下代码,并想使用 file_put_contents(); 将所有 $row 变量写入文本文件;我尝试执行以下操作,但它不起作用。我不确定我是否将它错误地放在大括号内:我正在尝试从一些视频中学习,但找不到任何有关如何插入查询的视频...

<?php

 include_once __DIR__.'/header2.php';

 $limit = 1;

if(!isset($_SESSION['u_uid'])) {
 echo "<meta http-equiv='refresh' content='0;url=index.php?update=notlogin'>";
  exit();
} else {
  include_once __DIR__.'/includes/dbh.php';





                           //Created a template
                            $sql = "SELECT * FROM users WHERE user_uid = ? LIMIT ?;";
                            //Create a prepared statement

                            $stmt = mysqli_stmt_init($conn);
                            //Prepare the prepared statement
                            if (!mysqli_stmt_prepare($stmt, $sql)) {
                                echo 'SQL statement failed';
                            } else {
                                 //Bind parameters to the placeholder
                                 mysqli_stmt_bind_param($stmt, "si", $_SESSION['u_uid'], $limit);
                                 //Run parameters inside database
                                 mysqli_stmt_execute($stmt);

                                    $result = mysqli_stmt_get_result($stmt);
                                    $resultcheck = mysqli_num_rows($result);


                                 if ($resultCheck > 0)  {




                                   $sql2 = "SELECT * FROM memberships WHERE user_uid = ? LIMIT ?;";
                            //Create a prepared statement

                            $stmt = mysqli_stmt_init($conn);
                            //Prepare the prepared statement
                            if (!mysqli_stmt_prepare($stmt, $sql2)) {
                                echo 'SQL statement failed';
                            } else {
                                 //Bind parameters to the placeholder
                                 mysqli_stmt_bind_param($stmt, "si", $_SESSION['u_uid'], $limit);
                                 //Run parameters inside database
                                 mysqli_stmt_execute($stmt);

                                    $result2 = mysqli_stmt_get_result($stmt);

                                    echo '<table class="update_information">

                                          <tr >
                                          <th class="update_title" colspan="2">Membership Plan Details</th>
                                          </tr>';





                                 while ($row2 = mysqli_fetch_assoc($result2))  {

                                    // Level 1 information

                                    if($row2['subscriptionplan'] == null) {
                                        $row2['subscriptionplan'] = 'None';
                                    } else {
                                        $row2['subscriptionplan'];

                                    }

                                 if ($row2['feesmonthly'] == 0) {
                                    $feesmonthly = 'None';
                                 } else {
                                    $feesmonthly = 100;
                                 }

                                 if ($row2['feesyearly'] == 0) {
                                    $feesyearly = 'None';
                                 } else {
                                    $feesyearly = 800;
                                 }

                                 if ($row2['emailreminder'] == 0) {
                                    $row2['emailreminder'] = 'None';
                                 } else {
                                    $row2['emailreminder'];
                                }

                                 if ($row2['overdue'] == 0) {
                                    $row2['overdue'] = 'None';
                                 } else {
                                    $row2['overdue'] = 'Yes';
                                 }

                                 if ($row2['subscriptionplandate'] == null) {
                                    $row2['subscriptionplandate'] = 'None';
                                 } else {
                                    $row2['subscriptionplandate'];
                                 }

                                 if ($row2['paid'] == null) {
                                    $row2['paid'] = 'Not Paid';
                                 } else {
                                    $row2['paid'] = 'Paid';
                                 }

                                 if ($row2['expirydate'] == null) {
                                    $row2['expirydate'] = 'None';
                                 } else {
                                    $row2['expirydate'];
                                 }

                                 if ($row2['paidbydate'] == null) {
                                    $row2['paidbydate'] = 'None';
                                 } else {
                                    $row2['paidbydate'];
                                 }

                                 if ($row2['activate'] == 0) {
                                    $row2['activate'] = 'Not Activated';
                                 } else {
                                    $row2['activate'] = 'Actviated';
                                }

                                // Level 2 information

                                 if($row2['subscriptionplan2'] == null) {
                                        $row2['subscriptionplan2'] = 'None';
                                    } else {
                                        $row2['subscriptionplan2'];

                                    }

                                 if ($row2['feesmonthly2'] == 0) {
                                    $feesmonthly2 = 'None';
                                 } else {
                                    $feesmonthly2 = 150;
                                 }

                                 if ($row2['feesyearly2'] == 0) {
                                    $feesyearly2 = 'None';
                                 } else {
                                    $feesyearly2 = 1300;
                                 }

                                 if ($row2['emailreminder2'] == 0) {
                                    $row2['emailreminder2'] = 'None';
                                 } else {
                                    $row2['emailreminder2'];
                                }

                                 if ($row2['overdue2'] == 0) {
                                    $row2['overdue2'] = 'None';
                                 } else {
                                    $row2['overdue2'] = 'Yes';
                                 }

                                 if ($row2['subscriptionplandate2'] == null) {
                                    $row2['subscriptionplandate2'] = 'None';
                                 } else {
                                    $row2['subscriptionplandate2'];
                                 }

                                 if ($row2['paid2'] == null) {
                                    $row2['paid2'] = 'Not Paid';
                                 } else {
                                    $row2['paid2'] = 'Paid';
                                 }

                                 if ($row2['expirydate2'] == null) {
                                    $row2['expirydate2'] = 'None';
                                 } else {
                                    $row2['expirydate2'];
                                 }

                                 if ($row2['paidbydate2'] == null) {
                                    $row2['paidbydate2'] = 'None';
                                 } else {
                                    $row2['paidbydate2'];
                                 }

                                 if ($row2['activate2'] == 0) {
                                    $row2['activate2'] = 'Not Activated';
                                 } else {
                                    $row2['activate2'] = 'Activated';
                                }

                                // Level 3 Information

                                if($row2['subscriptionplan3'] == null) {
                                        $row2['subscriptionplan3'] = 'None';
                                    } else {
                                        $row2['subscriptionplan3'];

                                    }

                                if ($row2['feesmonthly3'] == 0) {
                                    $feesmonthly3 = 'None';
                                 } else {
                                    $feesmonthly3 = 200;
                                 }

                                 if ($row2['feesyearly3'] == 0) {
                                    $feesyearly3 = 'None';
                                 } else {
                                    $feesyearly3 = 1800;
                                 }

                                 if ($row2['emailreminder3'] == 0) {
                                    $row2['emailreminder3'] = 'None';
                                 } else {
                                    $row2['emailreminder3'];
                                }

                                 if ($row2['overdue'] == 0) {
                                    $row2['overdue3'] = 'None';
                                 } else {
                                    $row2['overdue3'] = 'Yes';
                                 }

                                 if ($row2['subscriptionplandate3'] == null) {
                                    $row2['subscriptionplandate3'] = 'None';
                                 } else {
                                    $row2['subscriptionplandate3'];
                                 }

                                 if ($row2['paid3'] == null) {
                                    $row2['paid3'] = 'Not Paid';
                                 } else {
                                    $row2['paid3'] = 'Paid';
                                 }

                                 if ($row2['expirydate3'] == null) {
                                    $row2['expirydate3'] = 'None';
                                 } else {
                                    $row2['expirydate3'];
                                 }

                                 if ($row2['paidbydate3'] == null) {
                                    $row2['paidbydate3'] = 'None';
                                 } else {
                                    $row2['paidbydate3'];
                                 }

                                 if ($row2['activate3'] == 0) {
                                    $row2['activate3'] = 'Not Activated';
                                 } else {
                                    $row2['activate3'] = 'Activated';
                                }



                      echo '
                      <tr><th>Level 1 Membership Information:</th>
                      </tr>
                      <tr >
                      <th>Membership Plan:</th><td>',htmlspecialchars($row2['subscriptionplan']),'</td>
                      </tr>
                      <tr>
                      <th>Membership Plan Enrolled Date:</th><td>',htmlspecialchars($row2['subscriptionplandate']),'</td>
                      </tr>
                      <tr>
                      <th>Monthly Fees:</th><td>',htmlspecialchars($feesmonthly),'</td>
                      </tr>
                      <tr>
                      <th>Yearly Fees:</th><td>',htmlspecialchars($feesyearly),'</td>
                      </tr>
                      <tr>
                      <th>Payment Status:</th><td>',htmlspecialchars($row2['paid']),'</td>
                      </tr>
                      <tr>
                      <th>Expiry Date:</th><td>',htmlspecialchars($row2['expirydate']),'</td>
                      </tr>
                      <tr>
                      <th>Payment Due Date: </th><td>',htmlspecialchars($row2['paidbydate']),'</td>
                      </tr>
                      <tr>
                      <th>Email Reminder Status: </th><td>',htmlspecialchars($row2['emailreminder']),'</td>
                      </tr>
                      <tr>
                      <th>Over Due Status: </th><td>',htmlspecialchars($row2['overdue']),'</td>
                      </tr>
                       <tr>
                      <th>Activation Token: </th><td>',htmlspecialchars($row2['token']),'</td>
                      </tr>
                       <tr>
                      <th>Activate Status: </th><td>',htmlspecialchars($row2['activate']),'</td>
                      </tr>


                      <tr><th>Level 2 Membership Information:</th>
                      </tr>
                      <tr >
                      <th>Membership Plan:</th><td>',htmlspecialchars($row2['subscriptionplan2']),'</td>
                      </tr>
                      <tr>
                      <th>Membership Plan Enrolled Date:</th><td>',htmlspecialchars($row2['subscriptionplandate2']),'</td>
                      </tr>
                      <tr>
                      <th>Monthly Fees:</th><td>',htmlspecialchars($feesmonthly2),'</td>
                      </tr>
                      <tr>
                      <th>Yearly Fees:</th><td>',htmlspecialchars($feesyearly2),'</td>
                      </tr>
                      <tr>
                      <th>Payment Status:</th><td>',htmlspecialchars($row2['paid2']),'</td>
                      </tr>
                      <tr>
                      <th>Expiry Date:</th><td>',htmlspecialchars($row2['expirydate2']),'</td>
                      </tr>
                      <tr>
                      <th>Payment Due Date: </th><td>',htmlspecialchars($row2['paidbydate2']),'</td>
                      </tr>
                      <tr>
                      <th>Email Reminder Status: </th><td>',htmlspecialchars($row2['emailreminder2']),'</td>
                      </tr>
                      <tr>
                      <th>Over Due Status: </th><td>',htmlspecialchars($row2['overdue2']),'</td>
                      </tr>
                       <tr>
                      <th>Activate Token: </th><td>',htmlspecialchars($row2['token2']),'</td>
                      </tr>
                       <tr>
                      <th>Activate Status: </th><td>',htmlspecialchars($row2['activate2']),'</td>
                      </tr>


                      <tr>
                      <th>Level 3 Membership Information:</th>
                      </tr>
                      <tr>
                      <th>Membership Plan:</th><td>',htmlspecialchars($row2['subscriptionplan3']),'</td>
                      </tr>
                      <tr>
                      <th>Membership Plan Enrolled Date:</th><td>',htmlspecialchars($row2['subscriptionplandate3']),'</td>
                      </tr>
                      <tr>
                      <th>Monthly Fees:</th><td>',htmlspecialchars($feesmonthly3),'</td>
                      </tr>
                      <tr>
                      <th>Yearly Fees:</th><td>',htmlspecialchars($feesyearly3),'</td>
                      </tr>
                      <tr>
                      <th>Payment Status:</th><td>',htmlspecialchars($row2['paid3']),'</td>
                      </tr>
                      <tr>
                      <th>Expiry Date:</th><td>',htmlspecialchars($row2['expirydate3']),'</td>
                      </tr>
                      <tr>
                      <th>Payment Due Date: </th><td>',htmlspecialchars($row2['paidbydate3']),'</td>
                      </tr>
                      <tr>
                      <th>Email Reminder Status: </th><td>',htmlspecialchars($row2['emailreminder3']),'</td>
                      </tr>
                      <tr>
                      <th>Over Due Status: </th><td>',htmlspecialchars($row2['overdue3']),'</td>
                      </tr>
                       <tr>
                      <th>Activate Token: </th><td>',htmlspecialchars($row2['token3']),'</td>
                      </tr>
                       <tr>
                      <th>Activate Status: </th><td>',htmlspecialchars($row2['activate3']),'</td>
                      </tr>';






                                 }



                               //   $handle = fopen('invoice.txt', 'w+');

                                  file_put_contents($handle, $row2['subscriptionplan']);
                                   echo '</table>';

                               }
                             }
                         }
                     }

$invoice = "------------------------------------\n";

                            $invoice .= "Subscriptionplan:".$row['subscriptionplan']."\n"; 
                            $invoice .= "Enrollment Date:".$row['subscriptionplandate']."\n"; 
                            $invoice .= "Monthly Fees:".$row['feesmonthly']."\n"; 
                            $invoice .= "Payment Status:".$row['paid']."\n"; 
                            $invoice .= "Expiry Date:".$row['expirydate']."\n"; 
                            $invoice .= "Payment Due Date:".$row['paidbydate']."\n"; 
                            $h = fopen('invoice_level1monthly.txt', 'a');  
                            fwrite($h, $invoice); 
                            fclose($h); 

由于某种原因,$row 变量没有通过,我必须使用其他变量,例如 $price_level1monthly,但最新信息未附加到文件中,如图所示:

在此处输入图像描述

在此处输入图像描述

上面应该有两张图片

$path = getcwd()."/invoice/invoice_level1monthly.$_SESSION['u_uid']";

标签: phpmysqli

解决方案


推荐阅读