将MYSQLI查询结果写入php

时间:2016-01-21 11:24:29

标签: php html5 mysqli

<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的输出写入文本文件,但是我收到以下错误

enter image description here

文本文件应该有5行文本,如果我更改$txt = "$Channel_Location"我会有一个结果但不是全部5

4 个答案:

答案 0 :(得分:0)

在将字符串写入文件之前,必须连接数据。

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

然后您可以执行以下操作:

fwrite($myfile, $text);

答案 1 :(得分:0)

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

答案 2 :(得分:0)

fwrite将字符串写入文档。

$result是一个对象,因此不能写为字符串。

您最好的选择是当您通过循环打印输出值时,也将它们添加到字符串中。

我还建议在fwrite语句中执行if函数,因为您需要写入值,否则它只会打开文件,写一个空变量然后关闭。

<?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);

?>

答案 3 :(得分:0)

假设您要用户录制

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