Archive for Dev. (junyup2)

์ง€์‹์„ ์ฑ„์›Œ๊ฐ€๋Š” ใ€Ž๊ฐœ๋ฐœ์ž/ํ™”์ดํŠธํ•ด์ปคใ€๋ฅผ ๋ชฉํ‘œ๋กœ ์ •๋ฆฌํ•˜๋Š” ๋ธ”๋กœ๊ทธ

๋ชจ์˜ ํ•ดํ‚น ์Šคํ„ฐ๋””/๋ชจ์˜ ํ•ดํ‚น - ๊ณผ์ œ

[๊ณผ์ œ] 02์ฃผ์ฐจ(1) (์„ฑ์  ํ™•์ธ ํŽ˜์ด์ง€) - ํ•œ๊ธ€ ์˜ค๋ฅ˜ ๋ฐœ์ƒ

Gearvirus(junyup2) 2023. 11. 2. 03:03

๋ชจ์˜ ํ•ดํ‚น ์Šคํ„ฐ๋”” - 2์ฃผ์ฐจ ๊ณผ์ œ(1) (์„ฑ์  ํ™•์ธ์šฉ ํŽ˜์ด์ง€ ์ œ์ž‘)

ํ•œ๊ธ€์ž…๋ ฅ ์˜ค๋ฅ˜!

์ˆ˜์ •๋ณธ ๋งํฌ  ↓

๋ชจ์˜ ํ•ดํ‚น ์Šคํ„ฐ๋”” - ๊ณผ์ œ 02์ฃผ์ฐจ(1) (์„ฑ์  ํ™•์ธ ํŽ˜์ด์ง€) ์ˆ˜์ •๋ณธ

์ œ์ž‘ ์กฐ๊ฑด
  • ํ•™์ƒ ์ด๋ฆ„๊ณผ ์ ์ˆ˜๊ฐ€ ๋“ค์–ด ์žˆ๋Š” DB ์ƒ์„ฑ
  • GET ๋ฐฉ์‹์œผ๋กœ ๊ทธ ํ•™์ƒ์˜ ์ด๋ฆ„์„ ์ ์œผ๋ฉด ๊ทธ ํ•™์ƒ์˜ ์ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํŽ˜์ด์ง€ ์ œ์ž‘

์„ฑ์  ํ™•์ธ ํŽ˜์ด์ง€


ํ•™์ƒ · ์„ฑ์  DB ์ œ์ž‘

 

  • http://192.168.xxx.xxx:1018/phpmyadmin/
    • ip ์ฃผ์†Œ/phpmyadmin : MySql์„ ๊ด€๋ฆฌ ๊ฐ€๋Šฅํ•œ ํŽ˜์ด์ง€

  • ํ•™์ƒ ์ด๋ฆ„๊ณผ ์ ์ˆ˜๊ฐ€ ๋“ค์–ด์žˆ๋Š” DB ์ƒ์„ฑ
  • DB  ์ด๋ฆ„ score -> score_table ์ƒ์„ฑ
    • idx(PRIMARY) : int(10)
    • name : vachar(20) utf8_general_ci
    • score : int(10)

  • ์ด๋ฆ„ / ์„ฑ์  ์‚ฝ์ž…
    • INSERT INTO score_table value (NULL, 'ํ•™์ƒ์ด๋ฆ„', '์ ์ˆ˜')

score_table


๋™์ž‘ ๊ณผ์ •
  1. http://192.168.xxx.xxx:1018/score.php ์— ์ ‘์†
  2. ์„ฑ์ ์„ ํ™•์ธํ•˜๊ณ  ์‹ถ์€ ํ•™์ƒ ์ด๋ฆ„์„ ์ž…๋ ฅ
  3. ๊ฒฐ๊ณผ ํ™•์ธ
    • ํ•ด๋‹น ํ•™์ƒ์ด DB์— ์žˆ์œผ๋ฉด, ์ ์ˆ˜๋ฅผ ์ถœ๋ ฅ
    • ํ•ด๋‹น ํ•™์ƒ์ด DB์— ์—†์œผ๋ฉด, ํ•™์ƒ์˜ ์ •๋ณด๊ฐ€ ์—†์Œ์„ ์ถœ๋ ฅ

์ฝ”๋“œ ๋ชฉ๋ก
  • db_connection.php
  • score.php

db_connection.php

<?php
    define('DB_SERVER', 'localhost');
    define('DB_USERNAME', '*****');
    define('DB_PASSWORD', '***********');
    define('DB_NAME', 'score');
	
    $db_conn = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
?>

 

DB ์—ฐ๊ฒฐ

  • ๊ณ ์ • ๋ณ€์ˆ˜ ์ƒ์ˆ˜ํ™”
    • define('DB_SERVER', 'localhost'); : DB๊ฐ€ ๋™์ผ ์„œ๋ฒ„/์ปดํ“จํ„ฐ์— ์กด์žฌํ•˜๋ฏ€๋กœ 'localhost'
    • define('DB_USERNAME', '*****'); : DB ๋กœ๊ทธ์ธ์„ ์œ„ํ•œ ID
    • define('DB_PASSWORD', '***********'); : DB ๋กœ๊ทธ์ธ์„ ์œ„ํ•œ PW
    • define('DB_NAME', 'score'); : ์‚ฌ์šฉํ•  DB์˜ ์ด๋ฆ„

score.php

<?php
    //DB connect
    require_once("db_connection.php");

    if(!isset($_GET['student'])){
        echo "์ ์ˆ˜๋ฅผ ํ™•์ธํ•  ํ•™์ƒ์„ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.";
    }

    $sql = "select * from score_table where name='{$_GET['student']}'";
    $result = mysqli_query($db_conn, $sql);
    $row = mysqli_fetch_array($result);
    //var_dump($row);

    $student_name = $row['name'];
    $student_score = $row['score'];
    if(isset($student_name)){
        echo "<font size = 6>" ."$student_name ํ•™์ƒ์˜ ์ ์ˆ˜๋Š” $student_score ์ž…๋‹ˆ๋‹ค." ."</font>";
    }else{		
        if(isset($_GET['student'])){
            echo "<font size = 6>" ."{$_GET['student']} ํ•™์ƒ์˜ ์ •๋ณด๋Š” ์—†์Šต๋‹ˆ๋‹ค." ."</font>";
        }
    }
?>

<!DOCTYPE html>
<html lang="ko">
	<head>
	<meta charset="utf-8">
		<title>Student - Score</title>
	</head>
	<body>
		<form method="GET" action="">
			<br><br>
			<input type="text" name="student" placeholder="ํ•™์ƒ ์ด๋ฆ„ ์ž…๋ ฅ" autofocus>
		</form>
	</body>
</html>
  • require_once("db_connection.php"); : db_connection.php ๋ฅผ include ํ•˜์—ฌ DB์— ์—ฐ๊ฒฐ
  • if(!isset($_GET['student'])) : ์ž…๋ ฅ๋œ ์ •๋ณด๊ฐ€ ์žˆ๋Š”์ง€ ํ™•์ธ, ์—†๋‹ค๋ฉด ์ž…๋ ฅ์„ ์š”๊ตฌํ•˜๋Š” ๋ฌธ๊ตฌ๋ฅผ ์ถœ๋ ฅ
  • $sql = "select * from score_table where name='{$_GET['student']}'"; : ์‚ฌ์šฉํ•  SQL query
    • ์ž…๋ ฅํ•œ ํ•™์ƒ์— ํ•ด๋‹นํ•˜๋Š” db ํ…Œ์ด๋ธ”์˜ * (๋ชจ๋“  ์ •๋ณด)๋ฅผ ์š”์ฒญ
  • $result = mysqli_query($db_conn, $sql);  : SQL query๋ฅผ ์ด์šฉํ•˜์—ฌ ์ •๋ณด ์š”์ฒญ
  • $row = mysqli_fetch_array($result); : SELECT ๋ฌธ๊ตฌ์˜ Object๋กœ๋ถ€ํ„ฐ ํ•˜๋‚˜์˜ ๋ฐ์ดํ„ฐ set์„ ๊ฐ€์ ธ์˜ด
  • if(isset($student_name)) : ์š”์ฒญํ•œ ๊ฒฐ๊ณผ๊ฐ€ ์กด์žฌํ•œ๋‹ค๋ฉด, ํ•™์ƒ์˜ ์ด๋ฆ„๊ณผ ์ ์ˆ˜๋ฅผ ์ถœ๋ ฅ
  • if(isset($_GET['student'])) : ์ž…๋ ฅ์ด ์กด์žฌํ–ˆ์ง€๋งŒ, ์œ„์˜ ์š”์ฒญ ๊ฒฐ๊ณผ๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ์ •๋ณด๊ฐ€ ์—†์Œ์„ ํ‘œ๊ธฐ
  • <input type="text" name="student" placeholder="ํ•™์ƒ ์ด๋ฆ„ ์ž…๋ ฅ" autofocus> : ํ•™์ƒ ์ด๋ฆ„ ์ž…๋ ฅ์ฐฝ

์„ฑ์  ํ™•์ธ ํŽ˜์ด์ง€ : ์ž…๋ ฅ ์š”๊ตฌ ์ฐฝ
์„ฑ์  ํ™•์ธ ํŽ˜์ด์ง€ : ํ•™์ƒ์˜ ์ ์ˆ˜ ์ถœ๋ ฅ
์„ฑ์  ํ™•์ธ ํŽ˜์ด์ง€ : ํ•ด๋‹น ํ•™์ƒ ์กด์žฌ X


ํ›„๊ธฐ

 

๊ฒฐ๊ณผ ์ฐฝ์€ ๋ณด์ž˜๊ฒƒ ์—†์ง€๋งŒ, DB ์ ‘์† ๋ฐ ์ด์šฉ์— ๊ด€ํ•œ ๊ฐ„๋‹จํ•œ ํ…Œ์ŠคํŠธ๋ฅผ ํ•ด๋ณผ ์ˆ˜ ์žˆ๋Š” ๊ณผ์ œ ์˜€์Œ.

query ์š”์ฒญ์— ๊ด€ํ•œ ๊ณต๋ถ€๊ฐ€ ๋” ํ•„์š”ํ•˜๋‹ค๊ณ  ๋Š๋‚Œ.

 

 

์งˆ๋ฌธ ํ™˜์˜, ์ˆ˜์ • ๋ฐ ๋ณด์™„์— ๋Œ€ํ•œ ์ง€์  ํ™˜์˜