簡體   English   中英

將MYSQLI查詢結果寫入php中的文本文件

[英]Writing MYSQLI query results to a text file in php

<body>

  <?php

    ini_set('display_errors', 1);
        ini_set('display_startup_errors', 1);
        error_reporting(E_ALL);


      $servername = "localhost";
      $username = "root";
      $password = "";
      $dbname = "random";

      // Create connection
      $conn = mysqli_connect($servername, $username, $password, $dbname);
      // Check connection
      if (!$conn) {
          die("Connection failed: " . mysqli_connect_error());
      }

      $sql = "SELECT Channel_Location, Product, Active FROM channels
      ORDER BY RAND()
      Limit 5";
      $result = mysqli_query($conn, $sql);
      if (mysqli_num_rows($result) > 0) {
      // output data of each row
      while($row = mysqli_fetch_assoc($result)) {
          $Channel_Location = $row['Channel_Location'];
          echo "<tr><br><td>".$Channel_Location."</td></tr>";

      }

      } else {
          echo "0 results";
      }

          $myfile = fopen("Log.txt", "w") or die("Unable to open file!");
          $txt = "$result";
          fwrite($myfile, $txt);
          fclose($myfile);

      mysqli_close($conn);

?>

</body>

所以基本上我的問題是我試圖將$result的輸出寫入文本文件,但是出現以下錯誤

在此處輸入圖片說明

該文本文件應具有5行文本,如果我更改$txt = "$Channel_Location"我將得到一個結果,但不是全部5

您必須先連接數據,然后才能將字符串寫入文件。

$text = '';
while ($row = mysqli_fetch_assoc($result)) {
    $Channel_Location = $row['Channel_Location'];
    $text = $text . $Channel_Location . "\n";
}

然后,您可以執行以下操作:

fwrite($myfile, $text);
  $output = '';
  $outputArray=[];
  while($row = mysqli_fetch_assoc($result)) {
      $Channel_Location = $row['Channel_Location'];
      echo "<tr><br><td>".$Channel_Location."</td></tr>";
      $output .= $row['Channel_Location']."\n";
      $outputArray[] = $row;
  }

  file_put_contents('Log.txt',$output);

 // or you can use json_encode to save whole query result!!
 file_put_contents('log.txt',json_encode($outputArray));

fwrite將字符串寫入文檔。

$result是一個對象,因此不能寫為字符串。

最好的選擇是在循環中打印出值時,也將它們添加到字符串中。

我還建議您在if語句中執行fwrite函數,因為您將需要向其寫入值,否則它將只打開文件,寫入一個空變量然后關閉。

<?php

    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);


    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "random";

    // Create connection
    $conn = mysqli_connect($servername, $username, $password, $dbname);
    // Check connection
    if (!$conn) {
          die("Connection failed: " . mysqli_connect_error());
    }

    $sql = "SELECT Channel_Location, Product, Active FROM channels
    ORDER BY RAND()
    Limit 5";
    $result = mysqli_query($conn, $sql);
    if (mysqli_num_rows($result) > 0) {
        $output = '';
        // output data of each row
        while($row = mysqli_fetch_assoc($result)) {
            $Channel_Location = $row['Channel_Location'];
            echo "<tr><br><td>".$Channel_Location."</td></tr>";
            $output .= $Channel_Location . '/n';
        }

        $myfile = fopen("Log.txt", "w") or die("Unable to open file!");
        $txt = "$output";
        fwrite($myfile, $txt);
        fclose($myfile);

    } else {
      echo "0 results";
    }

    mysqli_close($conn);

?>

假設您要記錄用戶

$output ='';
if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        $Channel_Location = $row['Channel_Location'];
        $output .="<tr><br><td>".$Channel_Location."</td></tr>";

    }

} else {
      $output .= "0 results";
}

$myfile = fopen("Log.txt", "w") or die("Unable to open file!");
fwrite($myfile,$output);
fclose($myfile);

mysqli_close($conn);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM