커뮤니티

고용노동부, 산업인력공단과 함께하는 강원도 유일한 기업중심 IT전문교육기관 ICT융합캠퍼스만의 특별한교육입니다.
공인 IT숙련기술인의 다양한 접근방법으로 전문가다운 실무교육을 받을 수 있습니다.

Category

교육강좌

언어 PHP - MySQL 확장

페이지 정보

작성자 관리자 댓글 0건 조회 2,720회 작성일 20-06-10 13:37

본문

MySQL 확장

본 수업은 상위 수업인 데이터베이스 토픽의 후속 수업이기 때문에 데이터베이스 토픽을 먼저 보셔야 합니다.

MySQL 확장

PHP의 MySQL  확장 기능은 PHP 5.5부터 폐지 예정 상태(deprecated)가 되었다. 그 이후에는 폐지될 예정이기 때문에 신규 에플리케이션을 개발한다면 사용하지 안아야 한다. 또한 기존의 에플리케이션도 PDO나 mysqli로 이전을 추진해야 한다.

MySQL 확장은 MySQL을 사용하기 위한 확장 기능이다. 곧 폐지될 기능이지만, 기존의 에플리케이션에서 사용하고 있는 경우가 많이 있기 때문에 이에 대한 사용법을 익히는 것은 당분간은 중요할 것이다. 본 수업에서는 예제를 통해서 MySQL 확장을 어떻게 사용하는가를 알아보겠다. 

MySQL 트러블 슈팅

만약 MySQL을 사용하는 과정에서 아래와 같은 에러가 난다면 MySQL 익스텐션을 활성화해야 한다. Bitnami 윈도우 버전의 경우 아래와 같이 처리했다. 다른 운영체제는 이 방법을 참고한다.

1
Fatal error: Call to undefined function mysql_connect() in D:\BitNami\wampstack-5.4.12-0\apache2\htdocs\mysql\process.php on line 2

php의 디렉토리가 D:\BitNami\wampstack-5.4.21-0\php일 경우 D:\BitNami\wampstack-5.4.21-0\php\php.ini 파일을 열고 아래의 코드 앞의 ;을 제거한다.

1
extension=php_mysql.dll

그리고 익스텐션의 dll 파일이 설치된 디렉토리의 기본경로를 변경해준다. 아래의 경로는 필자가 설치한 Bitnami를 기준으로 한 것이기 때문에 사용자마다 다를 수 있다. 자신의 환경에 맞게 수정해준다.

1
extension_dir = "D:\BitNami\wampstack-5.4.21-0\php\ext"

예제

아래의 파일은 전송 버튼을 눌렀을 때 process.php 파일로 데이터를 전송한다.

input.php

1
2
3
4
5
6
7
8
9
10
11
12
13
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
</head>
<body>
<form action="./process.php?mode=insert" method="POST">
<p>제목 : <input type="text" name="title"></p>
<p>본문 : <textarea name="description" id="" cols="30" rows="10"></textarea></p>
<p><input type="submit" /></p>
</form>
</body>
</html>

아래의 파일은 데이터베이스를 조작하는 작업을 모아둔 PHP 에플리케이션이다. URL에 포함된 mode의 값에 따라서 다른 작업을 하도록 switch 문을 사용했다.

process.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
mysql_connect('localhost', 'root', '111111');
mysql_select_db('opentutorials');
switch($_GET['mode']){
case 'insert':
$result = mysql_query("INSERT INTO topic (title, description, created) VALUES ('".mysql_real_escape_string($_POST['title'])."', '".mysql_real_escape_string($_POST['description'])."', now())");
header("Location: list.php");
break;
case 'delete':
mysql_query('DELETE FROM topic WHERE id = '.mysql_real_escape_string($_POST['id']));
header("Location: list.php");
break;
case 'modify':
mysql_query('UPDATE topic SET title = "'.mysql_real_escape_string($_POST['title']).'", description = "'.mysql_real_escape_string($_POST['description']).'" WHERE id = '.mysql_real_escape_string($_POST['id']));
header("Location: list.php?id={$_POST['id']}");
break;
}
?>

아래의 파일은 데이터베이스에 저장된 토픽의 리스트와 선택된 토픽의 자세한 정보를 화면에 출력하는 에플리케이션이다.

list.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
mysql_connect('localhost', 'root', '111111');
mysql_select_db('opentutorials');
$list_result = mysql_query('SELECT * FROM topic');
if(!empty($_GET['id'])) {
$topic_result = mysql_query('SELECT * FROM topic WHERE id = '.mysql_real_escape_string($_GET['id']));
$topic = mysql_fetch_array($topic_result);
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<style type="text/css">
body {
font-size: 0.8em;
font-family: dotum;
line-height: 1.6em;
}
header {
border-bottom: 1px solid #ccc;
padding: 20px 0;
}
nav {
float: left;
margin-right: 20px;
min-height: 1000px;
min-width:150px;
border-right: 1px solid #ccc;
}
nav ul {
list-style: none;
padding-left: 0;
padding-right: 20px;
}
article {
float: left;
}
.description{
width:500px;
}
</style>
</head>
<body id="body">
<div>
<nav>
<ul>
<?php
while($row = mysql_fetch_array($list_result)) {
echo "<li><a href=\"?id={$row['id']}\">".htmlspecialchars($row['title'])."</a></li>"; }
?>
</ul>
<ul>
<li><a href="input.php">추가</a></li>
</ul>
</nav>
<article>
<?php
if(!empty($topic)){
?>
<h2><?=htmlspecialchars($topic['title'])?></h2>
<div class="description">
<?=htmlspecialchars($topic['description'])?>
</div>
<div>
<a href="modify.php?id=<?=$topic['id']?>">수정</a>
<form method="POST" action="process.php?mode=delete">
<input type="hidden" name="id" value="<?=$topic['id']?>" />
<input type="submit" value="삭제" />
</form>
</div>
<?php
}
?>
</article>
</div>
</body>
</html>

아래 파일은 토픽의 내용을 수정하기 위해서 사용할 에플리케이션이다.

modify.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php
mysql_connect('localhost', 'root', '111111');
mysql_select_db('opentutorials');
$result = mysql_query('SELECT * FROM topic WHERE id = '.mysql_real_escape_string($_GET['id']));
$topic = mysql_fetch_array($result);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
</head>
<body>
<form action="./process.php?mode=modify" method="POST">
<input type="hidden" name="id" value="<?=$topic['id']?>" />
<p>제목 : <input type="text" name="title" value="<?=htmlspecialchars($topic['title'])?>"></p>
<p>본문 : <textarea name="description" id="" cols="30" rows="10"><?=htmlspecialchars($topic['description'])?></textarea></p>
<p><input type="submit" /></p>
</form>
</body>
</html>

디버깅

  • 트위터로 보내기
  • 페이스북으로 보내기
  • 구글플러스로 보내기

답변목록

등록된 답변이 없습니다.