我使用下面的代码来制作一个HTML表。我在网上搜索PHP获取列标题,但找不到任何信息(只有行信息)。是否有可能使用while循环并从SQL中打印列标题,而不是将它们硬编码如下?
$stid = oci_parse($conn, "
SELECT *
FROM
(
SELECT orders.order_no, orders.porder_no, orders.date, order_totals.value
FROM orders, order_totals
WHERE orders.order_no = order_totals.order_no
AND orders.account_no = '" . $_SESSION['session_account'] . "'
ORDER BY orders.order_no DESC
)
WHERE ROWNUM <= 15
");
oci_execute($stid);
echo "<table class='table'>
<thread>
<tr>
<th>Order No</th>
<th>Purchase Order No</th>
<th>Date</th>
<th>Value</th>
</tr>
</thread>
<tbody>";
while ($row = oci_fetch_array($stid, OCI_NUM)) {
echo "<tr>";
echo '<td><a href="view.php?id=' . $row[0] . '">' . $row[0] . '</a></td>';
for ( $ii = 1; $ii < count($row); $ii++ ) {
echo "<td>" . $row[$ii] . "</td>";
}
echo "</tr>";
}
echo "</tbody> </table>";
使用这段代码,这可能会对您有所帮助,使用oci_field_name()
获取列名
echo "<table class='table'>
<thread>
<tr>";
$ncols = oci_num_fields($stid);// to get column number
for ($i = 1; $i <= $ncols; $i++) {// index start from 1
$column_name = oci_field_name($stid, $i);
echo "<th>".$column_name."</th>";
}
echo "</tr>
</thread>
<tbody>";
您正在寻找已经由PHP MySQL或MySQLI或PDO api提供的列名属性。
下面是一个例子- http://www.php.net/manual/en/function.mysql-field-name.php
好运。