메뉴 건너뛰기

프로그램언어

조회 수 30503 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
주석이 없다.. 나만 쓸려고 만들어 놓은거라서...
관련되서 공부하고자 하는 분들이 있으면 참고하시라...

#DBConn.Class.php
<? 
/*************************************************************** 
프로그램명 : DBConn Class 
작성자 : 윤영식 (2006-02-17 오후 5:07) 
설명 : DataBase Connection Class (Oracle, MySQL) 
***************************************************************/ 

Class DBConn { 

const MYSQL = 1; 
const ORACLE = 2; 

private $connection; 

private $dbType; 
private $dbHost; 
private $dbPort; 
private $dbSID; 
private $dbName; 
private $dbUser; 
private $dbPassword; 
private $dbCharset; 


function __construct($URI) { 

$this->parseURI($URI); 

try { 
$this ->Connection(); 

} catch (Exception $e) { 
echo $e->getMessage(); 

$this->__destruct(); 
} 
} 

function __destruct() { 

if ($this->dbType==MYSQL) { 
@mysql_close($this->connection); 
} else if ($this->dbType==ORACLE) { 
@oci_close($this->connection); 
} 
} 

private function parseURI($URI) { 

$URI = str_replace("#","_sp_",$URI); 
//$mysql_uri = "mysql://localhost:3306/?dbname=meris&username=meris&password=meris&charset=euckr"; 
//$oracle_uri = "oracle://localhost:1521/?sid=ora10g&username=meris&password=meris&charset="; 

$exp = parse_url ($URI); 

if (strtolower($exp[scheme])=="mysql") { 
$this->dbType=MYSQL; 
} else if (strtolower($exp[scheme])=="oracle") { 
$this->dbType=ORACLE; 
} 

$this->dbHost = $exp[host]; 
$this->dbPort = $exp[port]; 

parse_str($exp[query]); 

$this->dbSID = str_replace("_sp_","#",$dbsid); 
$this->dbName = str_replace("_sp_","#",$dbname); 
$this->dbUser = str_replace("_sp_","#",$username); 
$this->dbPassword = str_replace("_sp_","#",$password); 
$this->dbCharset = str_replace("_sp_","#",$charset); 
} 

function freeStatement($stmt) { 
if ($this->dbType==MYSQL) { 
@mysql_free_result($stmt); 
} else if ($this->dbType==ORACLE) { 
@oci_free_statement($stmt); 
} 
} 

private function Connection() { 
if ($this->dbType==MYSQL) { 
if (!$this->connection=@mysql_connect($this->dbHost,$this->dbUser,$this->dbPassword)) { 
throw new Exception(mysql_error()); 
} 

if(!mysql_select_db($this->dbName,$this->connection)){ 
throw new exception(mysql_error()); 
} 

if ($this->dbCharset) { 
@mysql_query("set session character_set_connection=".$this->dbCharset.";"); 
@mysql_query("set session character_set_results=".$this->dbCharset.";"); 
@mysql_query("set session character_set_client=".$this->dbCharset.";"); 

} 

} else if ($this->dbType==ORACLE) { 
if (!$this->connection = @oci_connect($this->dbUser,$this->dbPassword,$this->dbSID,$this->dbCharset)) { 
$error = oci_error(); 
throw new Exception($error[message]); 
} 

} else { 
throw new Exception("No such DB Type".$this->dbType); 
} 
} 

function getConnection() { 
return $this->connection; 
} 

function destroyConnection() { 
if ($this->dbType==MYSQL) { 
@mysql_close($this->connection); 
} else if ($this->dbType==ORACLE) { 
@oci_close($this->connection); 
} 
} 

function execute($sql,$flag=OCI_COMMIT_ON_SUCCESS) { 
if ($this->dbType==MYSQL) { 
if (!$stmt = @mysql_query($sql,$this->connection)) { 
echo mysql_error(); 
return false; 
} else { 
return $stmt; 
} 
} else if ($this->dbType==ORACLE) { 
if (!$stmt = @oci_parse($this->connection,$sql)) { 
$error = oci_error($this->connection); 
echo $error[message]; 
return false; 
} 

if (!@oci_execute($stmt,$flag)) { 
$error = oci_error($stmt); 
echo $error[message]; 
return false; 
} else { 
return $stmt; 
} 
} 
} 

function fetchObject(&$obj,$stmt) { 
if ($this->dbType==MYSQL) { 
if ($obj=@mysql_fetch_object($stmt)) { 
return true; 
} 
} else if ($this->dbType==ORACLE) { 

if ($row = @oci_fetch_assoc($stmt)) { 
$rows = Array(); 
while(list($key,$val)=each($row)) { 
$rows[strtolower($key)] = $val; 
} 
$obj = (object)$rows; 
return true; 
} 
} 
return false; 
} 

function fetchAssoc(&$obj,$stmt) { 
if ($this->dbType==MYSQL) { 
if ($obj = @mysql_fetch_assoc($stmt)) { 
return true; 
} 
} else if ($this->dbType==ORACLE) { 
if ($row = @oci_fetch_assoc($stmt)) { 
$obj = Array(); 
while(list($key,$val)=each($row)) { 
$obj[strtolower($key)] = $val; 
} 
return true; 
} 
} 

return false; 
} 

function fetchRow(&$obj,$stmt) { 
if ($this->dbType==MYSQL) { 
if ($obj = @mysql_fetch_row($stmt)) { 
return true; 
} 
} else if ($this->dbType==ORACLE) { 
if ($obj = @oci_fetch_row($stmt)) { 
return true; 
} 
} 

return false; 
} 

function fetchArray(&$obj,$stmt) { 
if ($this->dbType==MYSQL) { 
if ($obj = @mysql_fetch_array($stmt)) { 
return true; 
} 
} else if ($this->dbType==ORACLE) { 

if ($row = @oci_fetch_array($stmt)) { 
$obj = Array(); 
while(list($key,$val)=each($row)) { 
$obj[strtolower($key)] = $val; 
} 
return true; 
} 
} 

return false; 
} 

function commit() { 
if ($this->dbType==ORACLE) { 
return oci_commit($this->connection); 
} else { 
return true; 
} 
} 

function rollback() { 
if ($this->dbType==ORACLE) { 
return oci_rollback($this->connection); 
} else { 
return true; 
} 
} 

function getAffectedRows($obj="") { 
if ($this->dbType==MYSQL) { 
if ($obj=="") { 
return mysql_affected_rows(); 
} else { 
return mysql_affected_rows($obj); 
} 
} 

return false; 
} 

function getNumRows($stmt) { 
if ($this->dbType==MYSQL) { 
return mysql_num_rows($stmt); 
} 
return false; 
} 

## 계속 추가 예정 
} 
?> 


사용방법
Oracle DB 사용시 #oracle.php
<?php 
/*************************************************************** 
프로그램명 : Oracle 예제 
작성자 : 윤영식 (2006-10-18 오후 5:44) 
***************************************************************/ 
$uri = "oracle://localhost:1521/?sid=ora10g&username=meris&password=meris&charset="; 

// Class 호출 
$conn = new DBConn($uri); 

$sql = "SELECT name FROM ttable"; 

// 쿼리 실행 
$stmt = $conn->execute($sql); 

// $obj 오브젝트로 Fetch 
$conn->fetchObject(&$obj,$stmt); 

// $obj->컬럼명 하면 데이터 나옴 
$name = $obj->name; 

$obj=null; 

// Statment Free 
$conn->freeStatement($stmt); 
?> 


MySQL DB 사용시 #mysql.php
<?php 
/*************************************************************** 
프로그램명 : MySQL 예제 
작성자 : 윤영식 (2006-10-18 오후 5:47) 
***************************************************************/ 
$uri = "mysql://localhost:3306/?dbname=meris&username=meris&password=meris&charset=euckr"; 

// Class 호출 
$conn = new DBConn($uri); 

$sql = "SELECT name FROM ttable"; 

// 쿼리 실행 
$stmt = $conn->execute($sql); 

// $obj 오브젝트로 Fetch 
$conn->fetchObject(&$obj,$stmt); 

// $obj->컬럼명 하면 데이터 나옴 
$name = $obj->name; 

$obj=null; 

// Statment Free 
$conn->freeStatement($stmt); 
?> 


눈치 빠른 분들은 벌써 눈치 쳈겠지만...
$uri 값만 바까줌으로써.. MySQL 이든 Oracle 이든 모두 사용가능 하다.
Sybase 및 기타 다른 DB도 해보고 싶지만 설치 되어 있는 환경이 없어서 테스트 불가

List of Articles
번호 제목 날짜 조회 수
160 PHP의 유동변수!? - $a1 ~ $a2 같은 형식의 변수를 반복문 돌릴때... 2017.03.06 16610
159 PHP웹 보안 취약점 TOP5(웹해킹) 2023.01.12 292
158 PHP와 HTML과 자바스크립트의 관계 2021.03.26 311
157 PHP에서의 대칭 암호화/복호화 ― 간단한 예제에서 DB 입/출력까지 2018.09.14 3548
156 php에서 체크박스 선택한 것 보여주기 file 2019.01.08 1807
155 PHP에서 조건문 처리 2015.04.14 22038
154 PHP에서 자바스크립트 값 가져오기 2014.02.27 31635
153 PHP에서 자료, 데이터의 타입을 확인하는 방법, gettype() 2018.08.29 2447
152 PHP에서 암호화 encrypt 복호화 decrypt 해서 값을 넘기기 2018.02.09 10626
151 PHP에서 모든 세션 정보를 화면에 출력하는 방법 2018.08.29 2693
150 PHP에서 데이터를 엑셀(Excel)로 저장 2017.02.19 18428
149 PHP에서 UTF와 EUC-KR 변환 2019.02.19 1553
148 PHP에서 PDF파일 생성하기 2014.02.27 32777
147 PHP에서 Excel 파일을 만들 수 있는 PHPExcel file 2017.03.06 17112
146 PHP에서 CSV 파일 export file 2016.04.22 22335
145 PHP로 엑셀 자료 MySQL에 넣기 2017.03.06 17875
144 PHP로 Excel 파일 만들기... 2014.02.27 30257
143 php로 db 컨트롤 1 2017.03.06 15769
142 phpMyAdmin WebMysql 에 CSV 엑셀 파일 업로드 입력하기 ( Excel / Upload / data / 데이터 / 데이타 ) file 2021.03.25 760
141 phpexcel을 이용한 PHP로 엑셀파일 읽기와 생성 file 2017.03.06 22787
Board Pagination Prev 1 ... 5 6 7 8 9 10 11 12 13 14 ... 17 Next
/ 17

하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved