Here's a simple question.
I have worked on simple and small projects before where the performance didn't matter much, or at least it wasn't affected by repeating the same code twice.
I was following a bad practice whenever I needed to run a query and display the data on a webpage. I first ran the query and checked if it returned any results, if nrows > 0 then I would run the same query again and fetch the results using a while:
while ($row = oci_fetch_array ($stmt, OCI_BOTH)) {...}
(Of course, on the first query I would just retrieve one field on the database, not everything that I needed)
I really like using a while because it gives me flexibility to display the data wherever and whenever I want using
$row[i]
I am currently working on a larger project using an Oracle database, and a simple query can be pretty slow because the DB is pretty big, so now performance really matters.
I am running the query and then checking for the number of rows retrieved:
$stmt = oci_parse($conn, $query);
oci_execute ($stmt);
$nrows = oci_fetch_all($stmt, $results);
Then, if I get nrows > 0 I would use a foreach to display the results:
foreach ($results as $key => $val) {
echo '<th>' . $key . '</th>';
}
I don't really like this approach because the foreach loop doesn't give me the flexibility sometimes I need.
Question: In your opinion, what would be the best (faster, simpler, best performance and efficient) way of running a query, checking if it returns any results and then displaying the data without a foreach loop?