简体   繁体   English

Oracle / PHP oci_fetch_all()问题

[英]Oracle/PHP oci_fetch_all() issue

Pretty new to PHP using Oracle. 使用Oracle对PHP来说是新手。 I'm following examples online. 我在网上跟踪示例。 I'm using this example 1 from the official site. 我正在使用官方网站上的示例1。 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. 大多数DBMS(我都没有使用它们,所以我不能说“全部”)系统有一个单独的表来存储这种元信息,因此您需要查询它。 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 在Oracle中,它作为字典存储,可能存储在某种表头数据或元信息中(我真的不太了解Oracle的底层结构),它称为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. 但是,由于您已经知道列名并且可以轻松地将其硬编码进去,因此我不知道您为什么要在给出事实后查询此信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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