php oracleデータベースへの接続方法

12159 ワード

1.php.iniでextension=php_を開くoci 8拡張、サービス再開.
2.php/extディレクトリの下のphp_oci8.dllファイルをsystem 32ディレクトリにコピー
3.Oracle 9 iクライアントのシンバージョンをインストールしてから、コンピュータを再起動する
構成:
$config = array (

    'dbconfig' => 
          array (
            'db_host_name' => '192.168.2.197/orcl',
            'db_user_name' => 'zbkf',
            'db_password' => 'zbkf',
             
          ),
);

クエリー:
 
    //     
    $arr_result = array(); 
    $arr_result['result'] = 'false';  //true false     
    $arr_result['callerid'] = $callerid;  
     
    //      
    $db_host_name=$config['dbconfig']['db_host_name']; //'localhost/ORCL''
    $db_user_name=$config['dbconfig']['db_user_name'];//'asgr'
    $db_pwd=$config['dbconfig']['db_password']; //'asgr'

    //  Oracle
    $conn = oci_connect($db_user_name,$db_pwd,$db_host_name);//oci_connect('asgr','asgr','localhost/ORCL');
    if (!$conn) { 
        $e = oci_error(); 
        //print htmlentities($e['message']); 
        //WriteLog("  Oracle   ,oci_connect(".$db_user_name.",".$db_pwd.",".$db_host_name.")  ".htmlentities($e['message']));
        $arr_result['result'] = 'false';
        echo json_encode($arr_result);  //        
        return;
    } 
    else {
        //echo("    !");
        //$select = 'SELECT BL_TEL FROM CC_BLACKLIST'; //      
        $select = "Select count(BL_Tel) from CC_BlackList where BL_Tel like '%" . $callerid . "%' and BL_SFQY='0' ";
        //WriteLog($select);
        $result_rows = oci_parse($conn, $select); //   SQL  ,  SQL
        $row_count = oci_execute($result_rows, OCI_DEFAULT); //     OCI_DEFAULT      commit 
        //echo($row_count);
        if(!$row_count) { //   
            $e = oci_error($result_rows); 
            //echo htmlentities($e['message']); 
            //WriteLog("         !,oci_connect(".$db_user_name.",".$db_pwd.",".$db_host_name.")  ". $select."  ".htmlentities($e['message']));
            $arr_result['result'] = 'false';
            echo json_encode($arr_result);  //        
        } 
        /*
        //       
        while($row = oci_fetch_array($result_rows, OCI_RETURN_NULLS)) {
             if($row[0]==$callerid){
                $arr_result['result']='true';
                echo json_encode($arr_result);  //    
                exit;
             }
        }
        */
        $count=0;
        while($row = oci_fetch_array($result_rows, OCI_RETURN_NULLS)) {
            $count=$row[0];
            break;
        }
        //WriteLog($count);
        if($count>=1){
            $arr_result['result']='true';
        }
        else {
            $arr_result['result']='false';
        }
        echo json_encode($arr_result);  //        
    }

 
挿入:
    //  Oracle
    $conn = oci_connect($db_user_name,$db_pwd,$db_host_name);//oci_connect('asgr','asgr','localhost/ORCL');
    if (!$conn) { 
        $e = oci_error(); 
        //print htmlentities($e['message']); 
        WriteLog("  Oracle   ,oci_connect(".$db_user_name.",".$db_pwd.",".$db_host_name.")  ".htmlentities($e['message']));
        $arr_result['result'] = 'false';
        echo json_encode($arr_result);  //        
        return;
    } 
    else {
        //echo("    !");
        $sql = "insert into CC_LEAVEMESSAGE (ID,MESSAGEID,CALLINGNBR,LEAVEMESSAGETIME,FILENAME)  values (sys_guid(),'" . $SessionId . "','"  . $CallerId . "',sysdate,'" . $RecordFile . "')";
        //WriteLog($sql);
        $stid = oci_parse($conn, $sql); //   SQL  ,     
        $r = oci_execute($stid); //   SQL

        oci_free_statement($stid); 
        oci_close($con); //    
        //WriteLog($r);
        if(!$r) { 
            $e = oci_error($sql); 
            WriteLog("    Oracle   ,oci_connect(".$db_user_name.",".$db_pwd.",".$db_host_name.") " .$sql . '  '  .htmlentities($e['message']));
            $arr_result['result']='false';
        } 
        else{
            $arr_result['result']='true';
        }