简体   繁体   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>

So basically my issue is i'm trying to write the output of $result to a text file but i get the following error 所以基本上我的问题是我试图将$result的输出写入文本文件,但是出现以下错误

在此处输入图片说明

the text file should have 5 lines of text, if i change the $txt = "$Channel_Location" i will have one result but not all 5 该文本文件应具有5行文本,如果我更改$txt = "$Channel_Location"我将得到一个结果,但不是全部5

You must concatenate your data before writing the string to the file. 您必须先连接数据,然后才能将字符串写入文件。

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

You can then do something like: 然后,您可以执行以下操作:

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 writes a string to a document. fwrite将字符串写入文档。

$result is an object and as such cannot be written as a string. $result是一个对象,因此不能写为字符串。

Your best option is when you run through the loop to print out the values, also add them to a string. 最好的选择是在循环中打印出值时,也将它们添加到字符串中。

I would also recommend doing the fwrite function within the if statement as you will need values to write to it otherwise it will just open the file, write an empty variable and then close. 我还建议您在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);

?>

Suppose you want to user record 假设您要记录用户

$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