19. 데이터베이스(MySQL) 실습
https://www.opentutorials.org/course/1688/9398
1) MySQL 실습 1
PHP의 역할
- 데이터베이스 시스템과 웹서버 사이에서 중계자와 같은 역할
→ 처리하려는 .php 파일에 데이터베이스에 있는 정보를 가져와서 웹페이지로 만들라는 코드가 들어가 있다면, PHP가 중간에서 데이터베이스에 있는 정보를 가져와서 웹페이지를 만듬
→ 이런 맥락에서 PHP 애플리케이션을 미들웨어라고도 부름
mysqli
- PHP에 내장되어 있는 API
- PHP가 위와 같은 역할을 수행하기 위해 사용
실습환경 구축
|
MySQL monitor |
mysqli |
서버 접속 |
mysql -hlocalhost -uroot -p111111; |
$conn = mysqli_connect('localhost', 'root', '111111'); |
DB 선택 |
mysql> use opentutorials |
mysqli_select_db($conn, 'opentutorials'); |
조회 |
mysql> SELECT * FROM topic; |
$result = mysqli_query($conn, 'SELECT * FROM topic'); |
출력 |
$row = mysqli_fetch_assoc($result); $row['title']; $row['description']; |
※ 연관 배열
ex.php
1 2 3 4 5 6 7 8 9 10 11 | <?php // 일반 배열 $a = array("About JavaScript", "JavaScript is..."); echo $a[0]; echo "<br />"; // 연관 배열 $b = array("title" => "About JavaScript", "description" => "JavaScript is..."); echo $b["title"]; ?> | cs |
2) MySQL 실습 2
ex.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <?php $conn = mysqli_connect('localhost', 'root', ''); mysqli_select_db($conn, 'opentutorials'); $result = mysqli_query($conn, 'SELECT * FROM topic'); $row=mysqli_fetch_assoc($result); // 조회한 내용 중 첫번째 행의 데이터 echo $row['id']; echo $row['title']; echo "<br />"; $row=mysqli_fetch_assoc($result); // 조회한 내용 중 두번째 행의 데이터 echo $row['id']; echo $row['title']; echo "<br />"; $row=mysqli_fetch_assoc($result); // 조회한 내용 중 세번째 행의 데이터 echo $row['id']; echo $row['title']; echo "<br />"; $row=mysqli_fetch_assoc($result); // 조회한 내용 중 네번째 행의 데이터 -> 데이터 X var_dump($row); ?> | cs |
- php에서 NULL == False
index.php (database 부분 추가, <nav> 수정)
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 | <?php $conn = mysqli_connect('localhost', 'root', ''); mysqli_select_db($conn, 'opentutorials'); $result = mysqli_query($conn, 'SELECT * FROM topic'); ?> <!DOCTYPE html> <html> <head> <meta charset = "utf-8"> <link rel="stylesheet" type="text/css" href="/style.css"> </head> <body id="target"> <header> <img src="https://s3.ap-northeast-2.amazonaws.com/opentutorials-user-file/course/94.png" alt="생활코딩"> <h1><a href="/index.php">JavaScript</a></h1> </header> <nav> <ol> <?php while($row=mysqli_fetch_assoc($result)) { echo '<li><a href="/index.php?id='.$row['id'].'">'.$row['title'].'</a></li>'."\n"; } ?> </ol> </nav> <div id="control"> <input type="button" value="white" onclick="document.getElementById('target').className='white'"/> <input type="button" value="black" onclick="document.getElementById('target').className='black'"/> </div> <article> <?php if(empty($_GET['id']) == false) { echo file_get_contents($_GET['id'].".txt"); } ?> </article> </body> </html> | cs |
3) MySQL 실습 3
style.css (추가부분)
1 2 3 4 5 6 7 8 9 | nav ol{ list-style:none; padding:0; } article{ float:left; padding:20px; width:500px; } | cs |
index.php (<article> 수정)
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 | <?php $conn = mysqli_connect('localhost', 'root', ''); mysqli_select_db($conn, 'opentutorials'); $result = mysqli_query($conn, 'SELECT * FROM topic'); ?> <!DOCTYPE html> <html> <head> <meta charset = "utf-8"> <link rel="stylesheet" type="text/css" href="/style.css"> </head> <body id="target"> <header> <img src="https://s3.ap-northeast-2.amazonaws.com/opentutorials-user-file/course/94.png" alt="생활코딩"> <h1><a href="/index.php">JavaScript</a></h1> </header> <nav> <ol> <?php while($row=mysqli_fetch_assoc($result)) { echo '<li><a href="/index.php?id='.$row['id'].'">'.$row['title'].'</a></li>'."\n"; } ?> </ol> </nav> <div id="control"> <input type="button" value="white" onclick="document.getElementById('target').className='white'"/> <input type="button" value="black" onclick="document.getElementById('target').className='black'"/> </div> <article> <?php if(empty($_GET['id']) === false) { $sql='SELECT * FROM topic WHERE id='.$_GET['id']; $result = mysqli_query($conn,$sql); $row = mysqli_fetch_assoc($result); echo '<h2>'.$row['title'].'</h2>'; echo $row['description']; } ?> </article> </body> </html> | cs |
4) MySQL 실습 4
<p> 태그
- paragraph : 단락
<input type="text">
- 한 줄의 텍스트 입력창
<textarea> 태그
- 여러줄의 텍스트 입력창
<input type="submit">
- submit : 제출하다
- 사용자가 입력한 정보들을 서버로 전송
<form> 태그
- 입력 양식을 만들 때 사용
- 사용자가 입력한 정보를 서버로 전송할 때 사용하는 것
- 속성
* action : control들의 입력된 정보를 action 속성에 지정된 애플리케이션에게 전송
* method : 폼을 서버에 전송할 HTTP 메소드(GET 또는 POST)
※ GET 방식을 이용했을 때, 정보의 길이가 길면 오류 발생
→ 긴 정보는 URL에 입력값을 통해서는 전송할 수 없음
→ POST 방식 사용
3.html
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="/php/3.php" method="POST"> <p>제목 : <input type="text" name="title"></p> <p>본문 : <textarea name="description"></textarea></p> <input type="submit"> </form> </body> </html> | cs |
3.php
1 2 3 4 5 | <?php echo $_POST['title']; echo "<br>"; echo $_POST['description']; ?> | cs |
5) MySQL 실습 5
DESC [table 명];
- 해당 테이블의 구조를 기술해줌
now()
- MySQL의 함수
- MySQL이라는 데이터베이스가 제공하는 API
- 현재 날짜 및 시간 반환
index.php (control에 <a> 태그 추가)
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 | <?php $conn = mysqli_connect('localhost', 'root', ''); mysqli_select_db($conn, 'opentutorials'); $result = mysqli_query($conn, 'SELECT * FROM topic'); ?> <!DOCTYPE html> <html> <head> <meta charset = "utf-8"> <link rel="stylesheet" type="text/css" href="/style.css"> </head> <body id="target"> <header> <img src="https://s3.ap-northeast-2.amazonaws.com/opentutorials-user-file/course/94.png" alt="생활코딩"> <h1><a href="/index.php">JavaScript</a></h1> </header> <nav> <ol> <?php while($row=mysqli_fetch_assoc($result)) { echo '<li><a href="/index.php?id='.$row['id'].'">'.$row['title'].'</a></li>'."\n"; } ?> </ol> </nav> <div id="control"> <input type="button" value="white" onclick="document.getElementById('target').className='white'"/> <input type="button" value="black" onclick="document.getElementById('target').className='black'"/> <a href="write.php">쓰기</a> </div> <article> <?php if(empty($_GET['id']) === false) { $sql='SELECT * FROM topic WHERE id='.$_GET['id']; $result = mysqli_query($conn,$sql); $row = mysqli_fetch_assoc($result); echo '<h2>'.$row['title'].'</h2>'; echo $row['description']; } ?> </article> </body> </html> | cs |
write.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 | <?php $conn = mysqli_connect('localhost', 'root', ''); mysqli_select_db($conn, 'opentutorials'); $result = mysqli_query($conn, 'SELECT * FROM topic'); ?> <!DOCTYPE html> <html> <head> <meta charset = "utf-8"> <link rel="stylesheet" type="text/css" href="/style.css"> </head> <body id="target"> <header> <img src="https://s3.ap-northeast-2.amazonaws.com/opentutorials-user-file/course/94.png" alt="생활코딩"> <h1><a href="/index.php">JavaScript</a></h1> </header> <nav> <ol> <?php while($row=mysqli_fetch_assoc($result)) { echo '<li><a href="/index.php?id='.$row['id'].'">'.$row['title'].'</a></li>'."\n"; } ?> </ol> </nav> <div id="control"> <input type="button" value="white" onclick="document.getElementById('target').className='white'"/> <input type="button" value="black" onclick="document.getElementById('target').className='black'"/> <a href="write.php">쓰기</a> </div> <article> <form action="/process.php" method="post"> <p>제목 : <input typt="text" name="title"></p> <p>작성자 : <input typt="text" name="author"></p> <p>본문 : <textarea name="description"></textarea> <input type="submit" name="name"> </form> </article> </body> </html> | cs |
process.php
1 2 3 4 5 6 7 | <?php $conn = mysqli_connect("localhost", "root",""); mysqli_select_db($conn, "opentutorials"); $sql = "INSERT INTO topic (title,description,author,created) VALUES('".$_POST['title']."', '".$_POST['description']."', '".$_POST['author']."', now())"; $result = mysqli_query($conn, $sql); header('Location: /index.php'); // redirection ?> | cs |
'컴퓨터 > 웹 개발' 카테고리의 다른 글
[코딩야학] ~ 보안 - 18일차 (0) | 2017.08.16 |
---|---|
[코딩야학] ~ 관계형DB실습 - 17일차 (0) | 2017.08.11 |
[코딩야학] ~ DB이론 - 15일차 (0) | 2017.08.08 |
[코딩야학] ~ PHP 실습 - 13,14일차 (0) | 2017.08.08 |
[코딩야학] ~ JS실습 - 11,12일차 (0) | 2017.08.02 |