PHP

[PHP] 12. MySQL Extension ( MySQL 확장 )

러셀가의 집사 2017. 11. 26. 00:25


MySQL Extension






MySQL 확장은 MySQL 사용을 위한 확장 기능이다.





곧 폐지될 기능이지만, 기존의 에플리케이션에서 사용하고 있는 경우가 많이 있기 때문에,  



이에대하여 자세히 알아보자.




우선, MySQL확장에서의 추가(INSERT), 수정(UPDATE), 조회(SELECT), 삭제(DELETE)에 대하여 알아보자.



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>



위의 파일은 전송 버튼을 눌렀을 때 process.php 파일로 데이터를 전송하는 파일이다.




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>




위의 파일은 특정 연산을 처리하고도, 빈페이지나 연산결과를 출력하는 페이지가 아닌,



리스트들이 나타나있는 페이지로 이동시키는 코드를 가지고 있다.



이러한 방식을 Redirection이라고 한다.



if ( !empty )란 '존재한다면'과 같은 의미이므로, 잘 기억해두도록 하자.







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>


위의 파일은 modify 모드를 위한 첫번째 파일이다.



mysql_real_escaping_string( )이 url을 통한 해커의 공격을 방지하기 위한 반면,



htmlspecialchars()는 팝업창을 통한 해커의 공격을 방지하기 위함이다.







이와 같이, MySQL확장에서의 



추가(INSERT), 수정(UPDATE), 조회(SELECT), 삭제(DELETE)에 대하여 알아보았다.






출처 : opentutorials.org