2015-10-20 107 views
1

我有一個PHP編程的序列順序,我需要從表中查詢並將其插入到JSON數組中。我不知道我的錯誤在哪裏,但它只是沒有通過。請幫幫我,從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 [email protected]_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 是資源,在 ç空給出:\ XAMPP \ htdocs中\ WeltesFinance \頁面\ lib中\ DBFunction.php在線47上

警告:oci_execute()預計參數1是資源,在C空給出 :\對象(PurchaseOrder)#1(2){[「job」:「PurchaseOrder」:private] => string(1)「%」[「}」 subjob「:」PurchaseOrder「:private] => string(1)」%「}

我不知道到底哪裏我的錯誤是,請幫我

回答

1

UPDATE:
我做了一些更多的更新你的代碼和類,這裏的每個文件的相關變化。

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返回的實際連接資源開玩笑ction

請參見下面的在線評論

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; 
} 
+0

IM仍然有同樣的錯誤 –

+0

還是一樣的錯誤..也許弄錯somwhere別的? –

+0

我對答案進行了更新。我改變了你的初始'db'類。另外請注意,你並不需要對連接類進一步包裝。詳情請參閱評論。 –