U E D R , A S I H C RSS

ZP Board/PHP Study/MySQL


1. MySQL 사용하기

  • mysql_connect, mysql_close, mysql_query, mysql_affected_rows, mysql_num_rows, mysql_fetch_row, mysql_fetch_array
  • mysql_connect ("접속할 서버주소", "아이디", "패스워드");
    • mysql 에 접속한다.
  • mysql_close();
    • mysql 접속을 끊는다.
  • mysql_query("문장 끝의 세미콜론 없이 실행할 명령어 입력");
    • mysql 함수를 실행한다.
  • ex) $result = mysql_db_query("zp2002","select * from addressbook order by binary name");
    • $result 는 변수이름, 실행결과의 identifier를 받음
  • mysql_affected_rows
  • mysql_num_rows
  • mysql_fetch_row(실행결과의 identifier);
    • 실행결과의 identifier의 내용을 배열로 저장
      • ex) $row = mysql_fetch_array($result);
  • mysql_fetch_array(실행결과의 identifier);
    • mysql_fetch_row 의 확장판
    • 실행결과의 identifier의 내용을 field 명을 사용하여 배열로 저장

2. 사용예제

~cpp 
참고로 다음의 명령어들은 같은 실행결과를 보여준다..

1.
mysql_select_db("database1");
mysql_query("select * from table1 by order name");

2.
mysql_query("use database1");
mysql_query("select * from table1 by order name");

3.
mysql_db_query("database1", "select * from table1 by order name");

다음은 간단하게 만들어 본 주소록 소스이다..
~cpp 
<!--babo.php-->
<html>
<head>
<title> 주소록 </title>
</head>
<body>
<table border=1 cellpadding=2>
<tr align=center> <td> 이름 </td> <td width=120> 전화번호 </td> </tr>
<?php
mysql_connect ("165.194.17.15", "zp2002", "암호");
$result = mysql_db_query("zp2002","select * from addressbook order by binary name");
while($row = mysql_fetch_array($result)) {
	echo "<tr> <td>", $row["name"], "</td><td align=center>", $row["phone"], "</td> </tr>";
}
mysql_close();
?>
</table>

<FORM method="POST" action="save.php" >
이름 <INPUT type="text" name="name" value="" size=10 maxlength=10> 
전화번호 <INPUT type="text" name="phone" value="" size=15 maxlength=15> 
 <INPUT type="submit" value="입력" size=5>
</FORM> 

<font size=2 color=red>※ 이름과 전화번호를 모두 입력해야 저장이 됩니다.
<br>   이름 10bytes, 전화번호 15bytes 이내로 입력해주세요. </font>
</body>
</html>
{{{~cpp
<!save.php>
<php
if ($_POSTname != "" && $_POSTphone != "") {
mysql_connect("zeropage.org","zp2002","암호");
mysql_select_db("zp2002");
mysql_query("insert into addressbook values('$_POSTname', '$_POSTphone')");
mysql_close();
}
?>
<script language="javascript">window.location.replace("babo.php");</script>
}}} 위의 소스로 작성한 페이지이다. "주소록"


Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:28:30
Processing time 0.0177 sec