3

I have a prolem with this code

$stmt = oci_parse($db, $sql);
$isQueryOk = oci_execute($stmt);
if ($isQueryOk) {
    while (($row = oci_fetch_assoc($stmt)) != false) {
        array_push($results, $row);
    }
    echo json_encode($results);
} else {
    $msg = "Error FETCHING ALL [$sql] on " . mb_strtoupper($dbTable) . "!";
}

The problem is that if oci_fetch_assoc($stmt) return 20000 rows, the while (($row = oci_fetch_assoc($stmt)) != false) { array_push($results, $row); } takes to much time. Is there a way that i can return echo json_encode($results); without the WHILE cycle.

Thanks in advance.

2
  • Use oci_fetch_all: php.net/manual/en/function.oci-fetch-all.php Commented Oct 18, 2016 at 12:00
  • i've tried but cannot format as key=>value pair. Returns ex: {EMPRESA: ["CLCA", "CMIP", "CPP"], VAL: ["CLCA", "CMIP", "CPP"]} and i need [{EMPRESA: "CLCA", VAL: "CLCA"}, {EMPRESA: "CMIP", VAL: "CMIP"}, {EMPRESA: "CPP", VAL: "CPP"}] Commented Oct 18, 2016 at 12:16

3 Answers 3

2

OR try to use another way to push your array. "Note: If you use array_push() to add one element to the array it's better to use $array[] = because in that way there is no overhead of calling a function." http://php.net/manual/en/function.array-push.php

$results[] = $row;
Sign up to request clarification or add additional context in comments.

Comments

1

I'm not sure it'll be significantly faster, but as Marcos Sedrez wrote you can try using oci_fetch_all. You'll need to pass it a flag to return by row (instead of by column, the default) to match your current output format:

oci_fetch_all($stmt, $output, 0, -1, OCI_FETCHSTATEMENT_BY_ROW);
json_encode($output);

See the documentation for further information.

Comments

-1

The error message is not output anywhere, so if there was an error it won't be visible

try this one

$stmt = oci_parse($db, $sql);
if ($stmt === false) {
    $error = oci_error($db);
    $msg = "Error PARSING [$sql] on {$dbTable}: {$error['message']}";
} else {
    $isQueryOk = oci_execute($stmt);
    if ($isQueryOk) {
        $results = array();
        while (($row = oci_fetch_assoc($stmt)) !== false) {
            array_push($results, $row);
        }
        echo json_encode($results);
    } else {
        $error = oci_error($stmt);
        $msg = "Error EXECUTING [$sql] on {$dbTable}: {$error['message']}";
    }
}

if (isset($msg)) {
    error_log($msg);
    http_response_code(500);
    echo 'Internal Server Error';
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.