2016-01-29 30 views
0

我有以下代碼,但我不知道如何檢查插入是否成功。 execute返回resource id。我想檢查是否成功,並返回失敗的所有錯誤。如何檢查插入使用OCI成功

public function persist() 
    { 
    $update = FALSE; 
    if(!is_array($this->tablePrimaryKey)) { 
     if(!empty($this->fieldVals[$this->tablePrimaryKey])) { 
      $update = true;   
     } 
    } 

    if ($update) {  
     $sql = "UPDATE " . $this->tableName . " SET "; 
     $binds = []; 
     foreach ($this->fieldVals as $key=>$val) { 
     if ($key != $this->tablePrimaryKey) { 
      if(in_array($key, $this->DATE_IDS)) { 
       $sql .= '"' . strtoupper($key) . '" = sysdate,'; 
      } else { 
       $bind = 't_' . $key; 
       $binds[$bind] = $val; 
       $sql .= '"' . strtoupper($key) . '" = :' . $bind . ','; 
      } 
     } 
     } 
     $sql = substr($sql,0,-1); 

     $sql .= " WHERE " . $this->tablePrimaryKey . " = '" . $this->fieldVals[$this->tablePrimaryKey] ."'"; 
    } else { 
     $binds = $fields = $date_fields = []; 
     if(!empty($this->tablePrimaryKey) && !is_array($this->tablePrimaryKey)) { 
      $this->fieldVals[$this->tablePrimaryKey] = $this->generateNewPrimaryKey(); 
     } 
     foreach ($this->fieldVals as $key=>$val) { 

       $bind = ':t_' . $key; 
       if (in_array($key, $this->DATE_IDS)) { 
        $date_fields[] = strtoupper($key); 
       } else { 
        $binds[$bind] = $val; 
        $fields[] = strtoupper($key); 
       } 


     } 
     $sql = 'INSERT INTO ' . $this->tableName . '("' . implode('","', $fields); 

     if(count($date_fields) >0) { 
     $sql .= '","'; 
     $sql .= implode('","', $date_fields); 
     } 
     $sql.='") VALUES (' . implode(',', array_keys($binds)); 
     if(count($date_fields) >0) { 
     $cnt=0; 
     foreach($date_fields as $date) { 
      $cnt++; 
      if(preg_match('/NULL/i', $this->fieldVals[strtolower($date)], $result)) { 

       $sql .= ",NULL"; 

      } elseif(isset($this->fieldVals[strtolower($date)])) { 

       $sql .= ",TO_DATE('" . (new DateTime($this->fieldVals[strtolower($date)]))->format("Y-M-d H:i:s") . "', 'yyyy/mm/dd hh24:mi:ss')"; 
      } else { 
       $sql .= ",sysdate"; 
      } 

     } 
     } 
     $sql .= ')'; 
    } 

    $this->oiDb->parse($sql, $binds); 


    return $this->oiDb->execute(); 


    } 

我運行了$result = $oiRequests->hydrate($reportingRequest)->persist();$reportingRequest是關鍵值列/值的值對。 $result包含resource id$oiRequests是我的模特。

我已經試過

$num_rows = oci_fetch_assoc ($result); 

      print_r($num_rows); 

回報

Warning: oci_fetch_assoc(): ORA-24374: define not done before fetch or execute and fetch in /var/SP/oiadm/docroot/dev/uddins/requestportal/requestportal_ajax.php on line 65 
+0

最後插入新插入記錄的ID。 – Monty

+0

我如何用oracle執行此操作 – shorif2000

+0

從表名順序中選擇id by desc limit 1 ---->你的id必須是主鍵,自動增量。 – Monty

回答

0

大多數OCI功能上的錯誤返回false。這意味着您可以對返回值進行簡單檢查,如果是false,請致電oci_error()

對於檢查INSERT聲明是否奏效的具體情況,您可以參考example code for oci_commit()。該示例的相關部分重複此處:

// The OCI_NO_AUTO_COMMIT flag tells Oracle not to commit the INSERT immediately 
// Use OCI_DEFAULT as the flag for PHP <= 5.3.1. The two flags are equivalent 
$r = oci_execute($stid, OCI_NO_AUTO_COMMIT); 
if (!$r) {  
    $e = oci_error($stid); 
    trigger_error(htmlentities($e['message']), E_USER_ERROR); 
} 
相關問題