일일 정리

PHP - 오라클 DB 연동, 논리 모델링 구성

mysecurity 2025. 3. 24. 17:50

목차

1. PHP - 오라클 DB 연동

1-1 DB에서 브라우저로 데이터 출력

1-2 다양한 기능 추가 (삭제, 검색)

 

2. 논리 모델링 구성

2-1 관계

 

 

 

1. PHP - 오라클 DB 연동

1-1 DB에서 브라우저로 데이터 출력

지난 시간 개선한 st_vi.php는 읽으려는 테이블의 행의 개수만큼 while문을 반복하여 $row로 가져온다. 

이를 다시한번 개선하여 $result의 데이터를 $row로 한 번에 2차원 배열로 가져오도록 다음과 같이 개선할 수 있다.

// st_vi.php 개선 2 // 
 
 <?
  echo("<a href=./st_in.html>자료 입력하기</a><hr>");
  
  require('conn.php');
  
  $sql="select sno, sname, sex, major, syear, to_char(avr,'0.00') avr 
        from student order by sno";  
  
  $result=oci_parse($conn,$sql); 
  oci_execute($result);  
  $row_num=oci_fetch_all($result, $row);  // $row_num : 전제 행의 수
  oci_free_statement($result);  
  oci_close($conn);
  
  echo("Row의 개수는 $row_num 입니다.<br><hr>");
  
  echo("<table border='0'>");
  for($i=0; $i<$row_num; $i++) {
      echo("
            <tr>
               <td width='50'><p align='center'>{$row['SNO'][$i]}</p></td> 
               <td width='80'><p align='center'>{$row['SNAME'][$i]}</p></td>
               <td width='20'><p align='center'>{$row['SEX'][$i]}</p></td>
               <td width='20'><p align='center'>{$row['SYEAR'][$i]}</p></td>
               <td width='50'><p align='center'>{$row['MAJOR'][$i]}</p></td> 
               <td width='30'><p align='center'>{$row['AVR'][$i]}</p></td>
            </tr>
           "); 
  }       
  echo("</table>");
 ?>

 

11행 $row_num=oci_fetch_all($result, $row);

: $result의 데이터를 2차원 배열로 한 번에 가져온다.

 

※ $row 2차원 배열

  0 1 2
sno 201910 201911 201912
sname 짱구 맹구 철수
sex
major 생물 물리 화학
syear 4 4 4
avr 0.15 0.95 1.25

 

 

12, 13행 oci_free_statement($result);  oci_close($conn);

: $result의 데이터는 한 번에 모두 받아왔으므로 메모리에서 $result를 지우고 DB 연결을 끊는다.

 

데이터를 우리가 읽을 수 있게 배열로 받아오는 패치과정을 한 번만 진행하므로 개선 전에 비해 속도가 상당히 빠르나, 테이블에 데이터가 많을 경우 메모리를 매우 많이 쓰므로 실제로는 위험한 방법이다.

 

1-2 다양한 기능 추가 (삭제, 검색)

● 삭제 기능

st_vi.php에 다음과 같이 삭제 기능을 추가할 수 있다.

// st_vi.php 삭제 추가 //

<html>
    <head>
        <title>st_in.php</title>
        <meta http-equiv="content-type" content="text/html; charset=utf-8">
    </head>
<body>

<?
  echo("<a href=./st_in.html>자료 입력하기</a><hr>");
  require('conn.php');

  $del=$_GET['del']; 
  if (empty($del)){}    
  else {                             
        $sql="delete from student where sno='$del'"; 
        $result=oci_parse($conn,$sql);
        oci_execute($result);
        oci_free_statement($result);
       } 

  $sql="select sno, sname, sex, major, syear, to_char(avr,'0.00') avr 
        from student order by sno";     
  
  $result=oci_parse($conn,$sql);        
  oci_execute($result);

  $row_num=oci_fetch_all($result, $row);  // $row_num : 전제 행의 수
  oci_free_statement($result);  
  oci_close($conn);
  echo("Row의 개수는 $row_num 입니다.<br><hr>");
  
  echo("<table border='1'>");       
  for($i=0; $i<$row_num; $i++) {
      echo("
            <tr>
               <td width='80'><p align='center'>{$row['SNO'][$i]}</p></td> 
               <td width='80'><p align='center'>{$row['SNAME'][$i]}</p></td>
               <td width='80'><p align='center'>{$row['SEX'][$i]}</p></td>
               <td width='80'><p align='center'>{$row['SYEAR'][$i]}</p></td>
               <td width='80'><p align='center'>{$row['MAJOR'][$i]}</p></td> 
               <td width='80'><p align='center'>{$row['AVR'][$i]}</p></td>
               <td width='30'><a href=./st_vi.php?del={$row['SNO'][$i]}>del</a></td>
            </tr>
           "); 
  }
  echo("</table>");
?>
<br><hr><br>
<?  show_source(__FILE__); ?>
</body>
</html>

 

42행 <td width='30'><a href=./st_vi.php?del={$row['SNO'][$i]}>del</a></td>

: del에 'SNO행 $i열의 데이터' 즉, 자기 행의 학번을 담아서 st_vi.php를 다시 실행한다.

 

12 ~ 19행 $del=$_GET['del']; ~ else문

: del에 값이 없다면 아무 일도 일어나지 않으며, 값이 있다면(학번을 담아왔다면) 해당 학번이 있는 행을 삭제한다.

 

GET 방식은 변수의 데이터를 화면의 링크(url)를 통해 넘겨줄 때  사용한다.

POST 방식은 변수의 데이터를 본문(body)에 담아 보낸다.

 

● 검색 기능

st_vi.php에 다음과 같이 검색 기능을 추가할 수 있다.

// st_vi.php 검색 추가 //

<html>
    <head>
        <title>st_in.php</title>
        <meta http-equiv="content-type" content="text/html; charset=utf-8">
    </head>
<body>

<?
  echo("<a href=./st_in.html>자료 입력하기</a><hr>");
  require('conn.php');
// =========================== 삭제
  $del=$_GET['del']; 
  if (empty($del)){}    
  else {                             
        $sql="delete from student where sno='$del'"; 
        $result=oci_parse($conn,$sql);
        oci_execute($result);
        oci_free_statement($result);
       } 
// ===========================
// =========================== 검색
$search = $_POST['search'];
if (empty($search))                
   {
    $sql="select sno,sname,sex,major,syear,to_char(avr,'0.00') avr 
          from student order by sno";
   }   
else                                  
   { 
    $sql="select sno,sname,sex,major,syear,to_char(avr,'0.00') avr 
          from student 
          where sname like '%{$search}%' order by sno";
   }
// ===========================
  
  
  $result=oci_parse($conn,$sql);        
  oci_execute($result);

  $row_num=oci_fetch_all($result, $row);  // $row_num : 전제 행의 수
  oci_free_statement($result);  
  oci_close($conn);
  echo("Row의 개수는 $row_num 입니다.<br><hr>");
  
  echo("<table border='1'>");       
  for($i=0; $i<$row_num; $i++) {
      echo("
            <tr>
               <td width='80'><p align='center'>{$row['SNO'][$i]}</p></td> 
               <td width='80'><p align='center'>{$row['SNAME'][$i]}</p></td>
               <td width='80'><p align='center'>{$row['SEX'][$i]}</p></td>
               <td width='80'><p align='center'>{$row['SYEAR'][$i]}</p></td>
               <td width='80'><p align='center'>{$row['MAJOR'][$i]}</p></td> 
               <td width='80'><p align='center'>{$row['AVR'][$i]}</p></td>
               <td width='30'><a href=./st_vi.php?del={$row['SNO'][$i]}>del</a></td>
            </tr>
           "); 
  }
  echo("</table>");
?>
<form name='search' method='post' action='st_vi.php'>
검색창 &nbsp;<input type='text' name='search'>
             <input type='submit' name='확인' value='확인'>
</form>
<br><hr><br>
<?  show_source(__FILE__); ?>
</body>
</html>

 

61 ~ 64행 검색창 폼

: 원하는 문자열을 입력하여 search에 저장하고 st_vi.php를 다시 실행한다.

 

22 ~ 33행 $search = $_POST['search']; ~ else문

: search에 값이 없다면 기존대로 테이블을 출력하고, 값이 있다면(문자열을 담아왔다면) 해당 문자열이 있는 행만 출력한다.

: %{$search}%과 같이 따옴표 안에 변수값을 넣고 싶다면 중괄호를 쓰는게 좋다.

 

● 주식별자가 두 개인 테이블

학생이름, 과목번호, 점수로 이루어진 점수 테이블의 경우 학생이름과 과목번호를 동시에 주식별자로 사용한다. 이러한 경우 다음과 같이 출력 프로그램을 작성할 수 있다.

// sc_vi.php //

<html>
    <head>
        <title>sc_vi.php</title>
        <meta http-equiv="content-type" content="text/html; charset=utf-8">
    </head>
<body>

<?
  require('conn.php');

//======================== 삭제
  $del=$_GET['del']; 
  $del2=$_GET['del2'];
  if (empty($del) && empty($del2)){}    
  else {                             
        $sql="delete from score where sno='$del' and cno='$del2'"; 
        $result=oci_parse($conn,$sql);
        oci_execute($result);
        oci_free_statement($result);
       }
//======================== 
//========================검색
$search = $_POST['search'];
$search2 = $_POST['search2'];
if (empty($search) && empty($search2))                
   {   
    $sql="select sno, cno, result
          from score order by sno, cno";
   }   
else                                  
   { 
    $sql="select sno, cno, result
          from score 
          where sno like '%{$search}%' and cno like '%{$search2}%'order by sno, cno";
   }
//========================
  
  
  $result=oci_parse($conn,$sql);        
  oci_execute($result);

  $row_num=oci_fetch_all($result, $row);  // $row_num : 전제 행의 수
  oci_free_statement($result);  
  oci_close($conn);
  echo("Row의 개수는 $row_num 입니다.<br><hr>");
?>

<form name='search' method='post' action='sc_vi.php'>
검색창 
<br>
    sno:&nbsp;<input type='text' name='search'>
    cno:&nbsp;<input type='text' name='search2'>
    <input type='submit' name='확인' value='확인'>
</form>

<?
  
  echo("<table border='1'>
            <tr>
                <td width='80'><p align='center'>학번</p></td> 
                <td width='80'><p align='center'>과목번호</p></td>
                <td width='80'><p align='center'>점수</p></td>
            </tr>
        ");
    
  for ($i = 0; $i < $row_num; $i++) {
      echo("
               <tr> 
                  <td width='80'><p align='center'>{$row['SNO'][$i]}</p></td> 
                  <td width='80'><p align='center'>{$row['CNO'][$i]}</p></td>
                  <td width='80'><p align='center'>{$row['RESULT'][$i]}</p></td>
                  <td width='30'><a href=./sc_vi.php?del={$row['SNO'][$i]}&del2={$row['CNO'][$i]}>del</a></td>
               </tr>
              ");
  } 
  echo("</table>");
?>

<br><hr><br>
<?  show_source(__FILE__); ?>
<br><hr><br>
<?  show_source(__FILE__); ?>
</body>
</html>

 

st_vi.php 출력 화면

 

 

2. 논리 모델링 구성

2-1 관계

관계 그림

- 제품: 자회사에서 만들어 파는 물건

- 상품: 외부에서 사와 파는 물건

 

한 테이블에서 두 자식 테이블이 나오고, 두 자식의 데이터가 중복될 가능성이 없다면 좌측과 같이 반원에 x표시를 한 형태로 그려진다. 두 자식의 데이터가 서로 독립적이어야만 함을 나타내야 하므로 위와 같이 그리는 경우가 많다.

 

그와 반대로 두 자식의 데이터가 중복될 가능성이 있다면 우측과 같이 깔끔함 반원의 형태로 그려진다. 이 경우 두 자식의 데이터가 중복되든 말든 상관이 없으므로 위와 같은 표현보다는 테이블을 따로따로 그리는 경우가 많다.