Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将查询结果写入txt文件_Php_Sql_Oracle - Fatal编程技术网

Php 将查询结果写入txt文件

Php 将查询结果写入txt文件,php,sql,oracle,Php,Sql,Oracle,查询结果来自oracle如何在php中写入文本文件。这些代码写入html表。我希望实现为写入文本fle <?php $conn = oci_connect('hr', 'welcome', 'localhost/XE'); if (!$conn) { $e = oci_error(); trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR); }

查询结果来自oracle如何在php中写入文本文件。这些代码写入html表。我希望实现为写入文本fle

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

    ?>

您可以获取整个结果并用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
}


用法:

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

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

get_result('hello.txt'); // Read