尝试推送到OCI数据库时发生错误

时间:2019-05-02 06:15:31

标签: php sql oracle plsql

尝试根据错误日志中的数据发布到OCI数据库。

尝试执行时收到ORA-24374错误。

“警告:oci_fetch_array():ORA-24374:在第46行的C:\ xampp \ htdocs \ wam \ header.php中进行获取或执行并获取之前未完成定义”;

具有讽刺意味的是,它确实将数据发布到数据库。但是我仍然得到错误。并且需要刷新.php页面以获取其余代码的执行结果。

有问题的代码:

来自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);
    }

来自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', "");
}

从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);
        // }
    }

此OCI实例可用于许多不同的数据库操作。

我已经从另一个开发人员那里接手了这个项目,并且是php / OCI的新手。因此,我很乐意将header.php中的dbQuerySafe函数更改为任何内容,因为它已在整个项目中成功使用。

欢迎任何建议。

2 个答案:

答案 0 :(得分:0)

您正在尝试使用oci_fetch_array(将查询的下一行作为关联数组或数字数组返回)用于INSERT操作。只需尝试另一个功能,我想oci_execute将是合适的。

在这里您可以找到用于Oracle-PHP交互的所有函数。

https://www.php.net/manual/en/book.oci8.php

答案 1 :(得分:0)

因此,问题一直在于未正确配置函数以使其具有映射了值的数组。

tl:dr;这是我需要在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)
    }