简体   繁体   中英

Oracle/PHP oci_fetch_all() issue

Pretty new to PHP using Oracle. I'm following examples online. I'm using this example 1 from the official site. My issue is, it displays all the records like I want, but it's missing the column/field names. Does anyone now how to alter this so that it includes the headers? (ex, my employee table would have... First Name, Last Name....) Thanks

<?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 * FROM employee');
oci_execute($stid);

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

echo "$nrows rows fetched<br>\n";
var_dump($res);

// Pretty-print the results
echo "<table border='1'>\n";
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);

?>

Most DBMS(I haven't worked with them all so I can't say "all") systems have a separate table that stores this sort of meta information, so you'd want to query it. In Oracle, it's stored as a dictionary, probably in some sort of table header data or meta info (I really don't know Oracle's underlying structure well) and it's called user_tab_columns

So what do ya do? Query it as though it were a table.

SELECT * FROM user_tab_columns WHERE TABLE_NAME='employee'

However, I don't know why you'd want to query this after the fact given your code, as you already know the column names and could easily just hardcode them in.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM