ȸ¿ø°¡ÀԡžÆÀ̵ð/ºñ¹øã±â
ȨÀ¸·Î


ÃÊ°£´Ü php - oracle ¿¬µ¿ Ŭ·¡½º
13³â Àü
PHP¿¡¼­ ¿À¶óŬ ¿¬µ¿ÇÏ·Á¸é Á» °íÅ뽺·´´Ù.

¹º°¡ mySQLÀ̶û Á» ´Ù¸¥ ºÎºÐµµ ÀÖ´Â°Í °°°í..

ÇÏ¿© Ŭ·¡½º·Î ¸¸µé¾îº¸¾Ò´Ù.



class Oracle
{
  var $DBID = "id";
  var $DBPW = "pw";
  var $DBSID = "sid";

  var $conn;
  var $stmt;
        
  var $error = false;    // ¿¡·¯ ¹ß»ýÇϸé true ·Î ¼öÁ¤µÊ. commit,rollback °áÁ¤¿¡ »ç¿ë
  var $transaction = false;    // true ¸é auto commit ¾ÊÇÔ

  var $bind = array();
  var $data_size = array();

  // php4 ÀÇ »ý¼ºÀÚ
  function Oracle(){    
            
    $this->connect();
  }
  // php5 ÀÇ »ý¼ºÀÚ
  function __construct(){    
            
    $this->connect();
  }
  // php5 ÀÇ ¼Ò¸êÀÚ
  function __destruct(){    
            
    $this->disconnect();
  }

  function connect(){

    if(!$this->conn)
      $this->conn = OCILogon($this->DBID,$this->DBPW,$this->DBSID);
  }
        
  function disConnect(){

    if($this->stmt)
      @OCIFreeStatement($this->stmt);
    if($this->conn)
      @OCILogoff($this->conn);
  }
  // ¹ÙÀε庯¼ö °ª ÁöÁ¤
  // °°Àº °ªÀÌ¶óµµ executeDML() È£ÃâÀü¿¡ ¹Ýµå½Ã ¸Å¹ø È£ÃâÇØ¾ß ÇÔ(executeDML() ÇÔ¼öÈ£ÃâÈÄ ÃʱâÈ­µÇ¹Ç·Î)
  function setBind($bind){

    if(is_array($bind))
      $this->bind = $bind;
    else if($bind)
      $this->bind = array($bind);
  }
  // ¹ÙÀε庯¼ö »çÀÌÁî ÁöÁ¤. ÁöÁ¤¾ÈÇϸé Çش纯¼öÀÇ ÃÖ´ë»çÀÌÁî°¡ ±âº»°ªÀÓ
  function setDataSize($data_size){

    if(is_array($data_size))
      $this->data_size = $data_size;
    else if($data_size)
      $this->data_size = array($data_size);
  }
  // Äõ¸®¹® °á°ú¸¦ '´ÙÁ߹迭($rs[Çʵå¸í][À妽º])'·Î ¸®ÅÏÇÑ´Ù
  // $preferch_size´Â °¡Á®¿Ã ·¹ÄÚµå°Ç¼ö¸¦ ÁöÁ¤ÇÔ(¿É¼Ç)
  function selectList($query,$preferch_size=1){
            
    $this->connect();
    $this->stmt = OCIparse($this->conn,$query);

    if($this->stmt){

      $this->bindByName();                                                        
      $this->prefetch($preferch_size);                                        

      if($this->transaction){                        
        @OCIexecute($this->stmt,OCI_DEFAULT);
        $this->error();
      }else{
        @OCIexecute($this->stmt);
      }
      $rows = @OCIFetchStatement($this->stmt,$rs);                
    
      if($rows){                    
        if(!$this->transaction)
          @OCIFreeStatement($this->stmt);
        return $rs;
      }
    }            
    return array();
  }
  // Äõ¸®¹® °á°ú 1°ÇÀ» '¹è¿­($rs[Çʵå¸í or À妽º])'·Î ¸®ÅÏÇÑ´Ù
  // $option Àº OCI_ASSOC(Çʵå¸í) or OCI_NUM(À妽º) À» ÁöÁ¤(¿É¼Ç)
  function selectRow($query,$option=OCI_ASSOC){
            
    $this->connect();
    $this->stmt = OCIparse($this->conn,$query);

    if($this->stmt){

      $this->bindByName();

      if($this->transaction){                        
        @OCIexecute($this->stmt,OCI_DEFAULT);
        $this->error();
      }else{
        @OCIexecute($this->stmt);
      }
      if(@OCIFetchInto($this->stmt,$rs,$option+OCI_RETURN_NULLS)){
        if(!$this->transaction)
          @OCIFreeStatement($this->stmt);
        return $rs;
      }                    
    }            
    return array();
  }
  // ÇÑ°³ °ª¸¸ ¸®ÅÏÇÏ´Â Äõ¸®¹®À» ó¸®ÇÑ´Ù
  // ROWID,LOB,FILE µî ¿Ü¿¡´Â ½ºÆ®¸µÀ¸·Î ¹ÝȯÇÑ´Ù
  function selectOne($query){

    $this->connect();
    $this->stmt = OCIparse($this->conn,$query);        

    if($this->stmt){

      $this->bindByName();

      if($this->transaction){                        
        @OCIexecute($this->stmt,OCI_DEFAULT);
        $this->error();
      }else{
        @OCIexecute($this->stmt);
      }
      if(@OCIFetch($this->stmt)){

        // Àμö 1Àº Ä÷³¼ø¼­ À妽º(1ºÎÅÍ ½ÃÀÛÇÔ¿¡ ÁÖÀÇ!,Ä÷³¸í ÁöÁ¤µµ °¡´É)
        $value = @OCIResult($this->stmt,1);    
        if(!$this->transaction)
          @OCIFreeStatement($this->stmt);
        return $value;
      }
    }
  }
  // insert,update,deleteµîÀ» ½ÇÇàÈÄ ¿µÇâ¹ÞÀº ·Î¿ìÀÇ °¹¼ö¸¦ ¸®ÅÏÇÑ´Ù(auto commitÀÏ °æ¿ì¸¸ °¹¼ö¸¦ ¸®ÅÏÇÔ)
  // $transaction=true ÀÏ °æ¿ì ¸¶Áö¸·¿¡ ¹Ýµå½Ã commit() ÇÔ¼ö¸¦ È£ÃâÇÑ´Ù
  function executeDML($sql){

    $this->connect();
    $this->stmt = OCIparse($this->conn,$sql);    
            
    if($this->stmt){

      $this->bindByName();

      if($this->transaction){    // auto commit ÀÌ ¾Æ´Ò °æ¿ì
        @OCIexecute($this->stmt,OCI_DEFAULT);
        $this->error();
      }else{
        @OCIexecute($this->stmt);
        $num = @OCIRowCount($this->stmt);
        @OCIFreeStatement($this->stmt);
                    
        return $num;                    
      }
    }
  }
  // auto commit ¾ÊÇÏ°í ¸í½ÃÀûÀ¸·Î transaction ½ÃÀÛ
  // executeDML() È£ÃâÈÄ ¸¶Áö¸·¿¡ ¹Ýµå½Ã commit() ÇÔ¼ö¸¦ È£ÃâÇÑ´Ù
  function transaction(){

    $this->transaction = true;
  }
  // transaction ¿Ï·áÈÄ commit À̸é true¸¦ ¸®ÅÏÇÔ
  // executeDML() ¿¡¼­ ¿¡·¯°¡ Çϳª¶óµµ ¹ß»ýÇϸé ÀÚµ¿ rollback µÊ
  function commit(){

    if(!$this->error){
      @OCICommit($this->conn);
      $commit = true;
    }else{
      @OCIRollback($this->conn);
      $commit = false;
    }
    if($this->stmt)
      @OCIFreeStatement($this->stmt);

    $this->transaction = false;
    $this->error = false;

    return $commit;
  }
  // $transaction=true ÀÏ °æ¿ì ¸Å¹ø executeDML ¿¡¼­ ÀÚµ¿È£ÃâµÈ´Ù
  // ¿¡·¯ ¹ß»ýÇÒ °æ¿ì commit,rollbackÀÇ ÆǴܱâÁØÀÎ $error °ªÀ» º¯°æ
  function error(){

    if($error = @OCIError($this->stmt)){                
      //echo "<p> Error is : " . $error["code"] . " - " . $error["message"] . "<p>";
      $this->error = true;
    }
  }
  // :b1,:b2,:b3...¿¡ ¹ÙÀÎµå º¯¼ö ÁöÁ¤
  // ¹ÙÀε庯¼ö¸íÀº ¹Ýµå½Ã :b1,:b2,:b3... À¸·Î ÁöÁ¤ÇÑ´Ù
  function bindByName(){

    $size = sizeof($this->bind);

    for($i=0 ; $i < $size ; $i++){

      $ds = $this->data_size[$i];
      if(!$ds) $ds = -1;

      @OCIBindByName($this->stmt,":b".($i+1),$this->bind[$i],$ds);
    }
    $this->bind = array();                                                        
    $this->data_size = array();
  }
  // ¿À¶óŬ Ŭ¶óÀ̾ðÆ® ¹öÆÛ¿¡ ÀúÀåµÇ´Â ·¹ÄÚµåÀÇ ¼ö¸¦ ÁöÁ¤
  // ¿©·¯ ·¹Äڵ带 selectÇÒ °æ¿ì µðÆúÆ® °ªÀÌ À۾Ƽ­ ºñÈ¿À²ÀûÀÌ¸é °¡Á®¿Ã °Ç¼ö¸¸Å­ ÁöÁ¤
  function prefetch($preferch_size){

    if($preferch_size > 1)
      @OCISetPrefetch($this->stmt,$preferch_size);
  }
}

################# ¿©±â¼­ºÎÅÍ »ç¿ë¿¹ ÀÔ´Ï´Ù #################

1. ¿©·¯°ÇÀÇ ·¹Äڵ带 select ÇÒ °æ¿ì
$db = new Oracle();
$query = "select title,name,date from tb where ... »ý·«";
$rs = $db->selectList($query);
for( $i=0 ; $i < sizeof($rs["TITLE"]) ; $i++ ){
    echo $rs["TITLE"][$i] . $rs["NAME"][$i] . $rs["DATE"][$i] . "<br>";
}
* ÁÖÀÇÇÏ½Ç°Ç ²À ¹è¿­¿¡ ´ë¹®ÀÚ·Î ÀûÀ¸¼Å¾ß ÇÑ´Ù´Â °Ì´Ï´Ù

2. ÇÑ°ÇÀÇ ·¹Äڵ带 select ÇÒ °æ¿ì
$query = "select title,name,date from tb where rownum=1";
$rs = $db->selectRow($query);
echo $rs["TITLE"] . $rs["NAME"] . $rs["DATE"];

3. ÇÑ°³°ªÀ»  select ÇÒ °æ¿ì
$query = "select count(*) from tb";
$value = $db->selectOne($query);
echo "ÃÑ " . $value . " °Ç ÀÔ´Ï´Ù";

4. insert,update,delete ÇÒ °æ¿ì
$sql = "insert into tb (title,name) values ('$title','$name')";
$rs = $db->executeDML($sql);
if($rs){
echo $rs . " °ÇÀÇ µ¥ÀÌÅ͸¦ ó¸®ÇÏ¿´½À´Ï´Ù";
}    

5. Æ®·£Á§¼Ç ó¸®
$sql1 = "insert into tb1 ... »ý·«";
$sql2 = "insert into tb2 ... »ý·«";
$sql3 = "insert into tb3 ... »ý·«";
$db->transaction();
$db->executeDML($sql1);
$db->executeDML($sql2);
$db->executeDML($sql3);
$rs = $db->commit();
if($rs){
  echo "ó¸® ¼º°ø!";
}else{
  echo "¿¡·¯ ¹ß»ý!";
}
transaction() ÇÔ¼ö¸¦ È£ÃâÇϸé auto commit ÀÌ false °¡ µÇ¸ç ¸¶Áö¸·¿¡ commit() ÇÔ¼ö¸¦ È£ÃâÇÏ¸é ¿¡·¯°¡ Çϳª¶óµµ ¹ß»ýÇϸé ÀÚµ¿À¸·Î ·Ñ¹éµË´Ï´Ù ¹°·Ð ¿¡·¯°¡ ¾øÀ¸¸é Ä¿¹ÔµÇ±¸¿ä ÇÔ¼ö¸íÀÌ commit À̶ó°í ¹«Á¶°Ç Ä¿¹ÔµÇ´Â°Å ¾Æ´Õ´Ï´Ù ^^
transaction() ÇÔ¼ö¸¦ È£Ãâ¾ÊÇÏ°í executeDML()À» »ç¿ëÇϸé Ç×»ó auto commit ÀÔ´Ï´Ù

6. ¹ÙÀε庯¼ö »ç¿ë
$query = "select name from tb where id=:b1 and pw=:b2";
$bind = array($id,$pw);
$db->setBind($bind);
$name = $db->selectOne($query);
echo $name;

¹ÙÀÎµå º¯¼ö¸íÀº ²À :b1,:b2,:b3... ÀÌ·±½ÄÀ¸·Î ÁöÁ¤Çϸç setBind() ÇÔ¼ö¿¡ ¹è¿­·Î °ªÀ» ³Ñ°ÜÁÝ´Ï´Ù
¹ÙÀε庯¼ö°¡ :b1 ÇÑ°³¸¸ »ç¿ëµÈ °æ¿ì´Â ¹è¿­ÀÌ ¾Æ´Ï¶ó °ªÇϳª¸¸ ³Ñ°Üµµ µË´Ï´Ù(¾Æ·¡¿¹ ó·³)
³Ñ°ÜÁø °ªÀº selectXxx(),executeDML()µîÀÇ ÇÔ¼ö»ç¿ëÈÄ¿¡ ÃʱâÈ­µÇ¹Ç·Î ÇÔ¼ö ½ÇÇàÀü¿¡ ¸Å¹ø setBind() ÇÔ¼ö¸¦ È£ÃâÇØ¾ß ÇÕ´Ï´Ù
¾Æ·¡¿¹´Â Ʋ¸° »ç¿ë¿¹ÀÔ´Ï´Ù $cnt2 ¿¡´Â ¾Æ¹«°ªµµ ¾ø½À´Ï´Ù
$query1 = "select count(*) from tb1 where id=:b1";
$query2 = "select count(*) from tb2 where id=:b1";
$db->setBind($id);
$cnt1 = $db->selectOne($query1);
$cnt2 = $db->selectOne($query2);

¾Æ·¡¿¹°¡ ¿ÇÀº »ç¿ë¿¹ÀÔ´Ï´Ù
$db->setBind($id);
$cnt1 = $db->selectOne($query1);
$db->setBind($id);
$cnt2 = $db->selectOne($query2);

7. Æ®·£Á§¼Ç ó¸®¿Í ¹ÙÀÎµå º¯¼ö »ç¿ë
$sql1 = "insert into tb(title,name) values(:b1,:b2)";
$sql2 = "update tb2 set addr=:b1 where id=:b2";
$bind1 = array($title,$name);
$bind2 = array($addr,$id);
$db->transaction();
$db->setBind($bind1);
$db->executeDML($sql1);
$db->setBind($bind2);
$db->executeDML($sql2);
$rs = $db->commit();
if($rs){
  echo "ó¸® ¼º°ø!";
}else{
  echo "¿¡·¯ ¹ß»ý!";
}

ÃßõÃßõ : 302 Ãßõ ¸ñ·Ï
¹øÈ£ Á¦¸ñ
1,505
Java·Î ±¸ÇöÇÏ´Â °£´ÜÇÑ Client & Server ÇÁ·Î±×·¥
1,504
JDK ´Ù¿î·Îµå ¹× ¼³Ä¡ ¹æ¹ý
1,503
IIS¿¡¼­ JSP or Java Servlet µ¹¸®´Â ¹æ¹ý 1
1,502
ÅèĹ(Tomcat)ÀÇ ±â´É °³¿ä ¹× ¼³Ä¡ ¹æ¹ý
1,501
JSP ÆäÀÌÁö À̵¿ 4°¡Áö ¹æ¹ý ¹× Ư¼º
1,500
¼¼¼ÇÀ» ÀÌ¿ëÇÑ Ä«¿îÅÍ
1,499
ÆÄÀÏ¿¡¼­ ÇÑÁÙ¸¸ Àоî´Ù return ÇØÁÖ´Â ¼Ò½º
1,498
À¥ÆäÀÌÁö ±Ü¾î¼­ ŸÀÌƲ »Ñ·ÁÁÖ´Â ¼Ò½º
1,497
¾î¶² ÆĶó¸ÞÅÍ°¡ ³Ñ¾î¿Ô´ÂÁö ¾Ë¾Æ³»´Â ÇÔ¼ö
1,496
À½·Â-¾ç·Â º¯È¯±â
1,495
OpenSSL Example PHP Code - VerifySignature
1,494
Á¤¼ö ¿¬»ê½Ã ¹«Á¶°Ç ¿Ã¸², ¹«Á¶°Ç ¹ö¸², ¹Ý¿Ã¸² ó¸® ¹æ¹ý
1,493
À̹ÌÁö¸¦ ÁöÁ¤µÈ ºñÀ²·Î ÀÚ¸£±â (crop)
1,492
ÁöÁ¤³¯Â¥¿¡ À̹ÌÁö º¸¿©ÁÖ±â ȤÀº °¨Ãß±â
ÃÊ°£´Ü php-oracle ¿¬µ¿ Ŭ·¡½º
1,490
¼ÒÄÏÀ¸·Î ¸ÞÀϺ¸³»±â
1,489
escape(), unescape() - ¹®ÀÚ ¾Ïȣȭ
1,488
³»¿ëº¹»ç½Ã Ãâó ÀÚµ¿³Ö±â
1,487
´ë¹®ÀÚ, ¼Ò¹®ÀÚ º¯È¯ ÇÔ¼ö
1,486
PHP, Á¤±ÔºÐÆ÷ ³­¼ö »ý¼º, °¡¿ì½º ºÐÆ÷ ·£´ý ¹ß»ý ÇÔ¼ö; Normal Distribution
1,485
¸Å¹ø rand°á°ú°¡ °°°Ô ³ª¿ÀÁö ¾Ê°Ô ÇÏ·Á¸é
1,484
UTF-8·Î ÀÎÄÚµùµÈ ¹®ÀÚ¿­À» EUC-KR·Î ¹Ù²Ù´Â ¹æ¹ý
1,483
PHP¿¡¼­ URL Open½Ã ¿¡·¯°¡ ³¯¶§
1,482
Flash¿Í PHP¸¦ È°¿ëÇÏ¿© ¸ÖƼ ÆÄÀÏ ¾÷·Îµå ±¸ÇöÇÏ´Â ¹æ¹ý
1,481
mysql ¹é¾÷¿¡¼­ º¹±¸±îÁö
1,480
ƯÁ¤ ¹®ÀÚ¿­À» ±âÁØÀ¸·Î Çؼ­ ¹è¿­·Î ¸¸µé¾îÁÖ´Â ¹æ¹ý
1,479
Á¤±Ô½ÄÀ¸·Î ¹®ÀÚ¿­ ¹Ù²ãÁÖ±â
1,478
ƯÁ¤ Æ÷Å»¿¡¼­ Á¢¼ÓÇÑ »ç¿ëÀÚ¸¸ Á¢¼Ó ¸·´Â ¹æ¹ý
1,477
¹®ÀÚ¿­(string)À» ¹è¿­(array)·Î ¸¸µé¾îÁÖ´Â ¹æ¹ý
1,476
IP Address·Î ±¹°¡ ¾Ë¾Æ³»´Â ¹æ¹ý
¸ñ·Ï
¹ÂÁ÷Æ®·ÎÆ® ºÎ»ê±¤¿ª½Ã ºÎ»êÁø±¸ °¡¾ßµ¿ ¤Ó °³ÀÎÁ¤º¸Ãë±Þ¹æħ
Copyright ¨Ï musictrot All rights reserved.