PHP - Check whether oracle query return 0 row or not

1.5k views Asked by At

I do select from oracle database in PHP and I put the result array to variable. Here is the array format in my variable $day :

Array
(
    [START_EXECUTION] => Array
        (
            [0] => 20-NOV-14
            [1] => 21-NOV-14
            [2] => 22-NOV-14
        )

    [NUM_OF_POPULATION] => Array
        (
            [0] => 6355
            [1] => 6521
            [2] => 6740            
        )

    [SUM_AMOUNT] => Array
        (
            [0] => 366907360.38
            [1] => 386256312.15
            [2] => 404543462.06           
        )    
) 

But sometimes the query will return 0 row. So i should check whether the query return 0 row or not. If not, I will process the array and display in table form (work fine). But if it returns 0 row, I will display 'Data not found' (doesn't work)

Here is my code

<?php
    if(count($day)>0){ //Checking row num

    echo '<table class="table table-hover">';

    $cols = array_keys($day);

    echo '<tr>';
    foreach ($cols as $col) echo '<th>' . $col . '</th>';
    echo '</tr>';

    foreach ($day[$cols[0]] as $i => $null) {
        echo '<tr>';
        foreach ($cols as $col) echo '<td>' . $day[$col][$i] . '</td>';
        echo '</tr>';
    }

    echo '</table>';
    }
    else { // Display data not found
    echo '<div class="row well" style="text-align: center">';        
    echo '<h4>Data not found</h4>';
    echo '</div>';
 }
?>

Am I missed something in my code or do I make it in correct way? Appreciate for your help. Thank you

1

There are 1 answers

0
Simone Xavier On

I think the way you did is a good way.

But maybe the way you fill the array could be better (you didn't show how you did that).

To fill the array could be good to use oci_fetch_all (if you aren't using yet). That function return a array with all records at the same time so you can count the array returned by that function.