1일차 스터디 “Ch 01. 딱딱한 페이지에 생명 불어넣기”
PHP & MySQL
14.01.10
1. MySQL 설치 (데이터베이스 서버)
http://dev.mysql.com/ 다운로드 탭 ---> MySQL Community Server ---> Windows (x86, 64(or32)-bit).zip를 C:\ 에 다운
.zip 파일을 C:\ 에 풀고 환경 변수 등록 (시스템 ---> 고급 ---> 환경 변수)
폴더 안의 my.-dfault.ini 을 열어 다음과 같이 세팅하고 파일이름을 my.ini 으로 바꾼다.
cmd 창을 관리자 권한으로 연 뒤 ‘mysqld --install’ 로 MySQL을 Window Service에 등록한다.
‘net start mysql’ 을 입력하여 MySQL을 기동 시킨다
2. Apache 설치 (웹 서버)
http://mirror.apache-kr.org/ 에서 httpd/ ---> binaries ---> win32 ---> httpd-2.0.65-win32-x86-no_ssl.msi 다운로드 후 설치
3. PHP 설치 후 Apache와 연동
Apache의 httpd.conf 를 열어서 다음 코드를 추가
DirectoryIndex index.html index.html.var index.php
#PHP 5.2.17
LoadModule php5_module "C:\Program Files (x86)\php-5.2.17-Win32\php5apache2.dll"
AddType application/x-httpd-php .php .html .htm .php3 .php4 .php5 .inc .phtml
4. PHP 란?
- PHP는 웹 페이지가 클라이언트의 브라우저에 전송되기 전에 서버를 통해 페이지의 내용을 조작할 수 있도록 한다.
- PHP 스크립트는 서버에서 구동되며 HTML 코드를 바꾸거나 새로 만들기도 한다.
- 즉, PHP는 HTML에 관여하고 동적으로 만들어 내는 역할을 한다.
5. HTML 폼 예제
www.headfirstlabs.com/books/hfphp 에서 ch01 예제 다운로드
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Aliens Abducted Me - Report an Abduction</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<h2>Aliens Abducted Me - Report an Abduction</h2>
<p>Share your story of alien abduction:</p>
<form method="post" action="http://localhost/report.php"> ← post 메소드는 html 폼 데이터가 전송될 때 사용자는 변수들을 보지 못하게 브라우저에 표시한다.
action 속성이 html 폼과 php 스크립트를 연결한다
즉, html 폼이 서버로 전송되면 php 스크립트가 아파치로 구축한 서버에서 구동한다
<label for="firstname">First name:</label>
<input type="text" id="firstname" name="firstname" /><br />
<label for="lastname">Last name:</label>
<input type="text" id="lastname" name="lastname" /><br />
<label for="email">What is your email address?</label>
<input type="text" id="email" name="email" /><br />
<label for="whenithappened">When did it happen?</label>
<input type="text" id="whenithappened" name="whenithappened" /><br />
<label for="howlong">How long were you gone?</label>
<input type="text" id="howlong" name="howlong" /><br />
<label for="howmany">How many did you see?</label>
<input type="text" id="howmany" name="howmany" /><br />
<label for="aliendescription">Describe them:</label>
<input type="text" id="aliendescription" name="aliendescription" size="32" /><br />
<label for="whattheydid">What did they do to you?</label>
<input type="text" id="whattheydid" name="whattheydid" size="32" /><br />
<label for="fangspotted">Have you seen my dog Fang?</label>
Yes <input id="fangspotted" name="fangspotted" type="radio" value="yes" />
No <input id="fangspotted" name="fangspotted" type="radio" value="no" /><br />
<img src="fang.jpg" width="100" height="175"
alt="My abducted dog Fang." /><br />
<label for="other">Anything else you want to add?</label>
<textarea id="other" name="other"></textarea><br />
<input type="submit" value="Report Abduction" name="submit" />
</form>
</body>
</html>
6. PHP 스크립트 예제
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Aliens Abducted Me - Report an Abduction</title>
</head>
<body>
<h2>Aliens Abducted Me - Report an Abduction</h2>
<?php
$name = $_POST['firstname'] . ' ' . $_POST['lastname'];
$when_it_happened = $_POST['whenithappened'];
$how_long = $_POST['howlong'];
$how_many = $_POST['howmany'];
$alien_description = $_POST['aliendescription'];
$what_they_did = $_POST['whattheydid'];
$fang_spotted = $_POST['fangspotted'];
$email = $_POST['email'];
$other = $_POST['other'];
$to = 'kanghl1007@gmail.com';
$subject = 'Aliens Abducted Me - Abduction Report';
$msg = "$name was abducted $when_it_happened and was gone for $how_long.\n" .
"Number of aliens: $how_many\n" .
"Alien description: $alien_description\n" .
"What they did: $what_they_did\n" .
"Fang spotted: $fang_spotted\n" .
"Other comments: $other";
mail($to, $subject, $msg, 'From:' . $email); ← 이메일 전송 함수, 반드시 받는사람($to)와 제목($subject),
내용($msg)가 포함되어야 한다.
echo 'Thanks for submitting the form.<br />'; ← echo는 브라우저를 통해 정보를 출력해 주는 php 명령어
echo 'You were abducted ' . $when_it_happened;
echo ' and were gone for ' . $how_long . '<br />';
echo 'Number of aliens: ' . $how_many . '<br />';
echo 'Describe them: ' . $alien_description . '<br />';
echo 'The aliens did this: ' . $what_they_did . '<br />';
echo 'Was Fang there? ' . $fang_spotted . '<br />';
echo 'Other comments: ' . $other . '<br />';
echo 'Your email address is ' . $email;
?>
</body>
</html>
'데이터베이스 > MYsql' 카테고리의 다른 글
PHP&MySQL 4일차 스터디 (0) | 2014.02.14 |
---|---|
PHP&MySQL 3일차 스터디 (0) | 2014.02.14 |
PHP&MySQL 2일차 스터디 (0) | 2014.02.14 |
mysql 각종 오류들 (0) | 2011.03.08 |
MySQL 오류: ERROR 2003: Can't connect to MySQL server on 'localhost' (10061) 발생시 (1) | 2011.02.23 |