푸른청년 푸르게 살고있나?  
home | 살아가기 | news | 세상보기 | tip&tech | 방명록 |  
   전체
   asp
   php
   jsp
   mssql
   mysql
   informix
   linux
   unix
   win2000
   javascript
   html
   oracle
   java
   etc
    
:: Tip&Tech > php
[PHP]클래스 만들어 쓰기
php도  클래스를  만들어  쓸수  있다.  다들  알겠지만
보통 마니 쓰이는 함수들을 클래스로 만들어 필요할때 마다 불러쓰면 편리하다.
디비관련된거나 페이징 관련 함수들을 마니들 클래스로 만들어 쓴다.

디비 관련 클래스를 만들어보자.
php스쿨에 나와 있는것들을 참조했는데..
허접함을 미리 고백한다. 나에게 필요한 기능만을 만들었기 때문이고..
아직 검증이 안됬기 때문이다..
부디 참조만 하기 바란다.


dbclass.inc
<?

class MySqlDb{
var $Conn;
var $Host;
var $UserName;
var $Password;
var $DatabaseName;

var $Res;
var $Row;


//생성자
function MySqlDb($Host,$UserName,$Password,$DatabaseName){
$this->Host = $Host;
$this->UserName = $UserName;
$this->Password = $Password;
$this->DatabaseName = $DatabaseName;
}


//디비 연결
function Connect(){
$this->Conn = mysql_connect($this->Host,$this->UserName,$this->Password);

if(!$this->Conn){
$this->halt("Host를 연결할 수가 없습니다.");
}else{


if( !mysql_select_db($this->DatabaseName,$this->Conn) ){

$this->halt("DB를 연결할 수가 없습니다.");
}
}
}

//쿼리문을 이용하여 resultset 값을 만든다.
function Query($qry){

$this->Res = mysql_query($qry,$this->Conn);
if(!$this->Res){
$this->halt("쿼리문제 => $qry");
}

return $this->Res;

}

//resultset 값을 이용하여 실제 데이터 값을 배열로 리턴한다.
function Row($res){

$this->Row = mysql_fetch_row($res);
return $this->Row;
}


//resultset을 만들면서 생성된 메모리를 해제한다.
//@는 에러가 나도 넘긴다.
function FreeResult(){
@mysql_free_result($this->Res);

}

//디비연결을 해제한다.
function Disconnect(){

@mysql_close($this->Conn);

//메모리에서 변수들을 해제한다.
unset($this->Conn);
unset($this->Host);
unset($this->UserName);
unset($this->Password);
unset($this->DatabaseName);
unset($this->Res);
unset($this->Row);

}

// 에러메시지 출력함수
function halt($massage){

$this -> Errno = mysql_errno();
$this -> Error = mysql_error();

echo "<font color=red>Oops~!! Database Error</font> : $massage <p>" ;
echo "$this->Errno ($this->Error) <p>";
die ('process halted');
}


//filed에 따른 최대값을 구한다.
function MaxNum($fld,$tbn){

$qry = "select max($fld) from $tbn";
$res=$this->Query($qry);
$this->Row($res);

if($this->Row[0]){
$maxnum = $this->Row[0]+1;
}else{
$maxnum = 1;
}

$this->FreeResult();

return $maxnum;
}

//조건에 따른 카운트를 한다.
function Count($fld,$tbn,$where){
$qry = "select count($fld) from $tbn $where ";
$res=$this->Query($qry);
$this->Row($res);

$this->FreeResult();

return $this->Row[0];

}

}
?>

다음은 쓰는 방법을 설명한다.

<?
//디비관련 클래스가 있는 파일을 인클루드 한다.
include "dbclass.inc";

//클래스를 생성한다.
$mysql = new MySqlDb("localhost","계정아이디","패스워드","디비명");

//디비를 연결한다.
$mysql->Connect();


//Count함수를 써서 row수를 구한다.
$rows=$mysql->Count("num",$tbn,"where num = $num");

if($rows>0){
$query = "select num,targetdate,enddate,writer,subject,content,texthtml,filename,regdate,hits,ipAddr from $tbn where num=$num";

//쿼리문으로 resultset 을 만들고
$res = $mysql->Query($query);
//배열값을 리턴한다.
$row = $mysql->Row($res);


//각각의결과값들을 가져온다.
$num = $row[0];
$targetdate = $row[1];
$enddate = $row[2];
$writer = $row[3];
$subject = $row[4];
$content = $row[5];
$texthtml = $row[6];
$filename = $row[7];
$regdate = $row[8];
$hits = $row[9];
$ipAddr = $row[10];

//메모리에서 해제한다.
$mysql->FreeResult();

}

//디비연결을 해제한다.
$mysql->Disconnect();
?>

날짜: 2003-08-11 16:41:49, 조회수: 2852

다음글 [php+mysql]백만건 게시판 만들기
이전글 re:[php] php를 쉘상에서 실행하기

꼬리말
글쓴이 비밀번호 #스팸글방지(주인장 닉네임을 쓰시오)

  
since by 2003.03.23 / 3th 2005.07.26 / 4th 2009.04.22 made by bluesoul