【问题标题】:Querying JSON from PHP Function从 PHP 函数查询 JSON
【发布时间】:2016-01-18 14:54:36
【问题描述】:

我有一个 PHP 编程的序列顺序,我需要从表中查询并将其插入 JSON 数组。我不知道我的错误在哪里,但它只是没有通过。请帮帮我,

在 dbinfo.inc.php 中,

define("ORA_CON_UN", "ADMIN");
define("ORA_CON_PW", "pass");
define("ORA_CON_DB", "192.168.100.195/finance");

class db {
    private $conn;
    private $lastId;
    private static $instance;

private function _construct(){
    $this->connect();   
}

public static function create(){
    if (!isset(self::$instance)) {
        self::$instance = new db();
    }

    return self::$instance;
}

public function connect($dbconn = ORA_CON_DB, $dbuser = ORA_CON_UN, $dbpass = ORA_CON_PW){
    $this->conn = oci_connect($dbuser, $dbpass, $dbconn);
}
}

在 DBFunction.php 中

include 'dbinfo.inc.php';

class Connection{
    private $dbConnection;

    public function _construct($dbConnection){
        $this->dbConnection = $dbConnection;
    }

    function initConnection(){
        $db = new db();
        $dbConnection = $db->connect(); 
    }
}

class PurchaseOrder {
    private $job = '%';
    private $subjob = '%';

    public function _construct($job, $subjob){
        $this->job = $job;
        $this->subjob = $subjob;
    }

    function listPO($job, $subjob){

        $conn = new Connection();
        $conn->initConnection();
        $sql = oci_parse($conn, 'SELECT VPI.PO_NO FROM VW_PO_INFO@WENFINANCE_WENLOGINV_LINK WHERE VPI.PROJECT_NO = ' .$job. ' AND VPI.PROJECT_NAME =  ' .$subjob);

        if (!$conn) {
            $oerr = OCIError($conn); 
            trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
            exit();
        } else {
            echo 'CONNECTION SUCCEDED';
        }

        $rows = array();
        while ($r = oci_fetch_assoc($sql)){
            $rows[] = $r;
        } 
        $listPO = json_encode($rows);

        oci_execute($sql);
        oci_close($conn);
    }
}

最后,testDBFunction.php

include('DBFunction.php');

$queryOracle = new PurchaseOrder();

$queryOracle->listPO('W-IGG','');

var_dump($queryOracle);

这是我的错误信息,

警告:oci_parse() 期望参数 1 是资源,给定对象 在第 36 行的 C:\xampp\htdocs\WeltesFinance\pages\lib\DBFunction.php 连接成功警告:oci_fetch_assoc() 需要参数 1 是资源, null 给出 C:\xampp\htdocs\WeltesFinance\pages\lib\DBFunction.php 第 47 行

警告:oci_execute() 期望参数 1 是资源,给定 null 在第 52 行的 C:\xampp\htdocs\WeltesFinance\pages\lib\DBFunction.php object(PurchaseOrder)#1 (2) { ["job":"PurchaseOrder":private]=> string(1) "%" ["subjob":"PurchaseOrder":private]=> string(1) "%" }

我不知道我的错误在哪里,请帮助我

【问题讨论】:

    标签: php sql oracle


    【解决方案1】:

    更新:
    我对您的代码和类进行了更多更新,以下是每个文件的相关更改。

    dbinfo.inc.php

    <?php
    
    define("ORA_CON_UN", "ADMIN");
    define("ORA_CON_PW", "pass");
    define("ORA_CON_DB", "192.168.100.195/finance");
    
    Class DbConnect{
        private $user = null;
        private $password = null;
        private $db_string = null;
    
        public function __construct(){
            $this->user = ORA_CON_UN;
            $this->password = ORA_CON_PW;
            $this->db_string = ORA_CON_DB;
        }
    
        public function connect() {
            $connection = oci_pconnect($this->user, $this->password, $this->db_string);
            return $connection;
        }
    }
    ?>
    

    DBfunction.php

    <?php
    include 'dbinfo.inc.php';
    
    // there is no need for a Connection class unless you want further wrapping or something :-/
    
    Class PurchaseOrder {
    
        // ....
    
        public function listPO($job,$subjob){
            $db = new DbConnect();
            $conn = $DbConnect->connect();
    
            if (!$conn) {
                // keep your code, throw error, exit
            }
            else{
                // move all your database processing here
                $sql = oci_parse($conn, 'SELECT VPI.PO_NO FROM VW_PO_INFO...');
                // also note that you are passing an empty value for the $subjob parameter, thus making the query likely to fail
                oci_execute($sql);
    
                $rows = array();
                while($r = oci_fetch_assoc($sql)){
                    $rows[] = $r;
                }
                $listPO = json_encode($rows);
                oci_close($conn);
            }
    
            return $listPO; // you need to return $listPO in order to be able to dump it
        }
    
        // ....
    }
    

    testDBFunction.php

    <?php
    
    include('DBFunction.php');
    
    $queryOracle = new PurchaseOrder();
    
    // either pass what listPO() returns to a variable and dump it
    $jsonResults = $queryOracle->listPO('W-IGG','');    
    var_dump($jsonResults);
    
    // or dump the return directly
    // var_dump($queryOracle->listPO('W-IGG',''));
    

    需要在initConnection函数中返回实际的连接资源

    查看以下内联 cmets

    function initConnection(){
        $db = new db();
        // $dbConnection = $db->connect(); // this is not needed since you call connect from the 
                                        // constructor, but you need to return the connection
                                        // see below the EDIT
        // return the actual connection resource
        // return $dbConnection;
        return $db;
    }
    
    $sql = oci_parse($conn->initConnection(), 'SELECT VPI.PO_NO FROM VW_ ...')`
    

    编辑:

    public function connect($dbconn = ORA_CON_DB, $dbuser = ORA_CON_UN, $dbpass = ORA_CON_PW){
        $this->conn = oci_connect($dbuser, $dbpass, $dbconn);
        return $this->conn;
    }
    

    【讨论】:

    • 还是同样的错误.. 可能在其他地方出错了??
    • 我更新了答案。我改变了你最初的db 课程。另请注意,您实际上并不需要对连接类进行进一步的包装。有关详细信息,请参阅 cmets。
    猜你喜欢
    • 1970-01-01
    • 2011-06-04
    • 1970-01-01
    • 1970-01-01
    • 2021-04-29
    • 1970-01-01
    • 1970-01-01
    • 2021-03-15
    • 1970-01-01
    相关资源
    最近更新 更多