【问题标题】:Write query result to txt file将查询结果写入txt文件
【发布时间】:2016-09-07 03:38:24
【问题描述】:

查询结果来自oracle how to write to a text file in php.这些代码写入html表。我想实现为写文本文件。

<?php

    $conn = oci_connect('hr', 'welcome', 'localhost/XE');
    if (!$conn) {
        $e = oci_error();
        trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
    }

    $stid = oci_parse($conn, 'SELECT POSTAL_CODE, CITY FROM locations WHERE ROWNUM < 3');
    oci_execute($stid);

    $nrows = oci_fetch_all($stid, $res);

    foreach ($res as $col) {
        echo "<tr>\n";
        foreach ($col as $item) {
            echo "    <td>".($item !== null ? htmlentities($item, ENT_QUOTES) : "")."</td>\n";
        }
        echo "</tr>\n";
    }
    echo "</table>\n";

    oci_free_statement($stid);
    oci_close($conn);

    ?>

【问题讨论】:

    标签: php sql oracle


    【解决方案1】:

    你可以把整个结果写成 JSON:

    function save_result($result, $location) {
     $json = json_encode($result); // Convert $result into a json formatted string
     $file = fopen($location, 'w'); // Open the file to write
     fwrite($file, $json); // Write to file
     fclose($file); // Close up
    }
    

    function get_result($result, $location) {
     $file = fopen($location, 'r'); // Open the file to read
     $read = json_decode(fread($file, filesize($location))); // Decode json formated string into an asoc array
     fclose($location); // Close up
     return $read;
    }
    

    用法:

    save_result($result, 'hello.txt'); // Save
    
    get_result('hello.txt'); // Read
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-07
      • 1970-01-01
      • 2016-06-08
      • 1970-01-01
      • 2021-10-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多