2016-05-11 4 views
-2

クエリ結果は、oracleのphp.Theseコードでテキストファイルに書き込む方法からHTMLテーブルに書き込みます。書き込みテキストファイルとして実装します。クエリ結果をtxtファイルに書き込む

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

    ?> 

答えて

1

あなたは、全体の結果を取り、JSONでそれを書くことができ

function save_result($result, $location) { 
$json = json_encode($result); //Convert $result into a json formated string 
$file = fopen($location, 'w'); //Open the file to write 
fwrite($file, $json); //Wrtie 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 
関連する問題