Skip to content
Advertisement

Errors when trying to push to OCI db

Trying to post to OCI db based on data from an error log.

Get the ORA-24374 error when trying to execute.

“Warning: oci_fetch_array(): ORA-24374: define not done before fetch or execute and fetch in C:xampphtdocswamheader.php on line 46”;

Ironically, it does post data to the db. However I still get the error. And need to refresh the .php page to get result of the rest of the code to execute.

Code in question:

from header.php

    public function dbQuerySafe($statement, $format = '', $args = array(), $outvar = '')
    {
        $config = include('includes/config.php');
        $res = '';
        $connectionString = $config['database']['tnsstring'];
        $username = $config['database']['user'];
        $password = $config['database']['password'];
        $connobj = oci_connect($username, $password, $connectionString);
        $error = false;
        $result = false;

        if (!$connobj) {
            $e = oci_error();
            trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
            exit();
        }
        //print_r($statement);
        $stid = oci_parse($connobj, $statement);
        if ($stid) {
            if (!empty($format) && !empty($args)) {
                foreach ($args as $key => $val) {

                    oci_bind_by_name($stid, $key, $args[$key]);
                }
            }
            if ($format == 'insert' && !empty($outvar)) {
                oci_bind_by_name($stid, ':' . $outvar, $res, 32);
            }
            $rsltflag = oci_execute($stid);

            if ($rsltflag && $format != 'insert') {
                $result = array();
                while (($row = oci_fetch_array($stid, OCI_ASSOC + OCI_RETURN_NULLS)) != false) {
                    foreach ($row as $key => $val) {
                        $c[$key] = $val;
                    }
                    $result[] = $c;
                }
            } else if ($rsltflag && $format == 'insert') {
                $result = $res;
            } else {
                $result = false;
                $error = oci_error($stid);

                $log = new Logger('header.php- line 57');
                $log->pushHandler(new StreamHandler(dirname(__FILE__) . './log/db_queries.log', Logger::ERROR));
                // $log->error($error['message'] . '. SQL query which caused the error: ' . $error['sqltext']);
                $log->error("Error connecting to database: ", array('username' => 'test username', 'module' => 'header.php', 'page_name' => 'header.php', 'error_at' => 'line 57', 'severity' => 'ERROR', 'error_msg' => $error['message']));

                trigger_error(htmlentities($error['message'], ENT_QUOTES), E_USER_ERROR);
            }
        }

        oci_free_statement($stid);
        oci_close($connobj);
        return array($result, $error);
    }

From error_log.php:

if (isset($_POST['push_to_db'])) {

    $handle = @fopen($errorFile, "r");
    $values = '';

    while (!feof($handle)) {
        $buffer = fgets($handle, 4096);

        $errorUsername = $error_api->str_slice($buffer, strpos($buffer, '"username"') + 12, strpos($buffer, '","module"'));
        $errorModule = $error_api->str_slice($buffer, strpos($buffer, '"module"') + 10, strpos($buffer, '","page_name"'));
        $errorPageName = $error_api->str_slice($buffer, strpos($buffer, '"page_name"') + 13, strpos($buffer, '","error_at"'));
        $errorErrorAt = $error_api->str_slice($buffer, strpos($buffer, '"error_at"') + 12, strpos($buffer, '","severity"'));
        $errorSeverity = $error_api->str_slice($buffer, strpos($buffer, '"severity"') + 12, strpos($buffer, '","error_msg"'));
        $errorErrorMsg = $error_api->str_slice($buffer, strpos($buffer, '"error_msg"') + 13, strpos($buffer, '"}'));


        if ($errorUsername) {
            echo ("<script>console.log('" . $errorUsername . "');</script>");
            echo ("<script>console.log('" . $errorModule . "');</script>");
            echo ("<script>console.log('" . $errorPageName . "');</script>");
            echo ("<script>console.log('" . $errorErrorAt . "');</script>");
            echo ("<script>console.log('" . $errorSeverity . "');</script>");
            echo ("<script>console.log('" . $errorErrorMsg . "');</script>");
            $error_api->pushErrorsToDB($errorUsername, $errorModule, $errorPageName, $errorErrorAt, $errorSeverity, $errorErrorMsg);
        }
        //for testing purposes
        // else {
        //     echo ("<script>console.log('" . $errorUsername . "');</script>");
        // }
    }

    // clear log file
    file_put_contents('../log/db_queries.log', "");
}

From api.php:

    public function pushErrorsToDB($username, $module, $pageName, $errorAt, $severity, $errorMsg)
    {
        //function to push Monolog files to WAM_ERROR_LOG table


        list($q, $error) = $this->dbQuerySafe(
            "
            INSERT INTO WAM_ERROR_LOG
            (USERNAME, MODULE, PAGE_NAME, ERROR_AT, SEVERITY, ERROR_MSG)
            VALUES
            ('$username', '$module', '$pageName', '$errorAt', '$severity', '$errorMsg')"
        );

        // if (!$error) {
        //  return array($q, false);
        // } else {
        //  return array(false, $error);
        // }
    }

This OCI instance has worked fine for many different db operations.

I have taken this project over from another developer and I am new to php/OCI. So I am reticent to change anything to the dbQuerySafe function from header.php as it is used successfully throughout the project.

Any advice welcome.

Advertisement

Answer

So the issue had been not correctly configuring the function to have an array with the values mapped.

tl:dr; this is the code I needed to change in api.php:

    public function pushErrorsToDB($username, $module, $pageName, $errorAt, $severity, $errorMsg)
    {
        //function to push Monolog files to WAM_ERROR_LOG table


        list($q, $error) = $this->dbQuerySafe(
            "
            INSERT INTO WAM_ERROR_LOG
            (USERNAME, MODULE, PAGE_NAME, ERROR_AT, SEVERITY, ERROR_MSG)
            VALUES
            (:username, :module, :pageName, :errorAt, :severity, :errorMsg)",
            'insert',
            array(':username' => $username, ':module' => $module, ':pageName' => $pageName, ':errorAt' => $errorAt, ':severity' => $severity, ':errorMsg' => $errorMsg)
        );

        if (!$error) {
            return array($q, false);
        } else {
            return array(false, $error);
        }

        // "INSERT INTO case_history(case_id, workstatusid, roleid, modifiedby, modifieddate, ownerid) values (
        //  :caseid, :workstatus, :function_id, :modifiedby, SYSDATE, :owner)",
        //  'sssss',
        //  array(':caseid' => $caseid, ':workstatus' => $workstatus, ':function_id' => $function_id, ':modifiedby' => $userid, ':owner' => $owner)
    }
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement