** 여기서는 mysql 함수, html, php 문법들이 마구마구 나오는 실습정리 글입니다. 피드백은 언제나 환영입니다.
이걸 진행하기 전에 1. kdhong 계정을 생성하고, 2. kdhong_db 데이터베이스를 생성, 3. stud_score 테이블을 생성했습니다. 자세한 방법은 mysql 카테고리에 나와있습니다.
결과물)

(예시를 위해 '이유진' 의 성적을 입력해놓은 상태입니다! )
목차)
1. php , mysql 연동
2. php 순위 매기기
3. 웹페이지 만들기
<?
$con = mysql_connect("localhost","kdhong","1234");
mysql_select_db("kdhong_db",$con);
<? : php문 시작
1. mysql_connect : PHP와 MySQL을 연결한다. ‘C:\>mysql -ukdhong -p1234’와 같은 기능.
mysql_select_db : 사용할 데이터베이스를 선택 ‘mysql>use kdhong_db;’와 같은 명령이다.
if ($mode=="insert"){
$sum = $sub1+$sub2+$sub3+$sub4+$sub5;
$avg = $sum/5;
$sql = "insert into stud_score (name, sub1, sub2, sub3, sub4, sub5, sum, avg) values ('$name','$sub1','$sub2','$sub3','$sub4','$sub5','$sum','$avg')";
$result = mysql_query($sql, $con);
밑에서 웹 설계하는 과정의 <form ~ 문에서 mode=insert 일 때, 밑에 문을 실행합니다.
성적 입력 후 '입력하기' 버튼을 누르면 stud_score 이라는 테이블에 입력값을 저장합니다.
mysql_query($a,$b) : 함수의 인자에 있는 SQL 명령을 실행합니다.
<meta charset="euc-kr">
<title>성적입력기</title>
<h2>1) 성적 입력하기</h2>
<form name="stud_score" method="post" action="score_list.php?mode=insert">
<table width="720" border="1" cellpadding="5"><tr>
<td>이름 :<input size="6" name="name"type="text"></td>
<td>과목1 :<input size="6" name="sub1"type="text"></td>
<td>과목2 :<input size="6"name="sub2"type="text"></td>
<td>과목3 :<input size="6" name="sub3"type="text"></td>
<td>과목4 :<input size="6" name="sub4"type="text"></td>
<td>과목5 :<input size="6" name="sub5"type="text"></td>
<td><input type="submit" value="입력하기"></td></tr></table></form>
meta charset ~ : 태그는 html 파일의 인코딩을 알려주는 태그입니다. 인코딩을 명확하게 알려주지 않으면 웹브라우저 설정 상황에 따라 자동으로 인코딩을 추정해서 처리해주는데, 처리가 정확할 경우도 있지만, 그렇지 못하는 경우도 많습니다. 다양한 경우에 한글이 깨지지 않고 잘 보이기를 기대한다면 위 태그는 꼭 적어주는 것이 좋습니다.
<p><h2>2) 성적 출력 하기</h2>
<p> <a href="score_list.php?mode=big_first">[성적순 정렬]</a>
<a href ="score_list.php?mode=small_first">[성적역순 정렬]</a><p>
성적 출력 제목을 넣어줍니다.
<a href ~> mode=big_first 가장 큰 성적순으로 나열하는 로직을 첨부합니다.
mode=small_first 역순으로 나열하는 로직을 첨부합니다.
<table width="720" border="1" cellpadding="5">
<tr align="center" bgcolor="#eeeeee">
<td>번호</td>
<td>이름</td>
<td>과목1</td>
<td>과목2</td>
<td>과목3</td>
<td>과목4</td>
<td>과목5</td>
<td>합계</td>
<td>평균</td>
<td>순위</td>
<td> </td>
</tr>
두번째 테이블을 만들어줍니다. 위에 나와있는 것 중 자세한 설명은 html 카테고리 글에 자세히 나와있습니다.
<?
if ($mode =="big_first")
$sql = "select * from stud_score order by sum desc";
elseif ($mode =="small_first")
$sql = "select * from stud_score order by sum";
else
$sql = "select * from stud_score";
$result = mysql_query($sql);
for($count=1; $count<=mysql_num_rows($result);$count++){
$row = mysql_fetch_array($result);
$avg = round($row[avg], 1);
$num = $row[num];
echo ("<tr align='center'>
<td> $count </td>
<td> $row[name] </td>
<td> $row[sub1] </td>
<td> $row[sub2] </td>
<td> $row[sub3] </td>
<td> $row[sub4] </td>
<td> $row[sub5] </td>
<td> $row[sum] </td>
<td> $avg </td>
<td> $row[rank]</td>
<td> <a href='score_delete.php?num=$num'>[삭제]</a></td>
</tr>");
}
mysql_close();
?>
</table>
'RDB > MySQL' 카테고리의 다른 글
mysql 기본 명령어, 기본 함수 (0) | 2021.01.21 |
---|---|
MySQL 실습 (3) - 필드 추가,삭제,데이터 변경. desc,alter,drop,where (0) | 2021.01.14 |
MySQL 기초실습(2) - 지정한 DB에 table 생성, 데이터 넣기 (0) | 2021.01.14 |
MySQL 기초 실습 (1) - 계정 생성하기 select,create 등 (0) | 2021.01.13 |