user3485445
user3485445

Reputation: 33

How to get a specific column from PHP oracle query?

I am pulling all the records from a table using the php code below, in the emp table there is a field called hire date that is UTC time, I want to be able to take the UTC from the table and display in a 12 hour AMPM format. So without changing my select statement, when using php how do I know when I have come to a certain column and I want to do something with that data?

<?php
include 'dB/oraConfig.php';


$query = 'select * from emp where rownum <80';
$stid = oci_parse($conn, $query);
oci_execute($stid);

echo "<table border='1'>\n";
$ncols = oci_num_fields($stid);
echo "<tr>\n";
for ($i = 1; $i <= $ncols; ++$i) {
    $colname = oci_field_name($stid, $i);
    echo "  <th><b>".htmlentities($colname, ENT_QUOTES)."</b></th>\n";
}
echo "</tr>\n";

while (($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) != false) {
     echo "<tr>\n";
     foreach ($row 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);

Upvotes: 1

Views: 3215

Answers (2)

user1601973
user1601973

Reputation: 572

From PHP documentation,

$stid = oci_parse($conn, 'SELECT department_id, department_name FROM departments');
oci_execute($stid);

while (($row = oci_fetch_array($stid, OCI_BOTH)) != false) {
    // Use the uppercase column names for the associative array indices
    echo $row[0] . " and " . $row['DEPARTMENT_ID']   . " are the same<br>\n";
    echo $row[1] . " and " . $row['DEPARTMENT_NAME'] . " are the same<br>\n";
}

I was wrong. You're are already in a while loop so you don't need to do another loop. Moreover, you can read about oci-fetch-array here.

Upvotes: 2

Mario Gonzales Flores
Mario Gonzales Flores

Reputation: 705

foreach inside while?

while (($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) != false) {
    echo "<tr>\n";
    echo "  <td>".($row['column_name'] !== null ? htmlentities($row['column_name'], ENT_QUOTES):" ")."</td>\n"; 
    echo "</tr>\n";
}

Upvotes: 0

Related Questions