+++ 20220126/DB.php
... | ... | @@ -0,0 +1,22 @@ |
1 | +<?php | |
2 | + | |
3 | +class DB | |
4 | +{ | |
5 | + private $user = "root"; | |
6 | + private $password = "1234"; | |
7 | + private $option = array(PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION); | |
8 | + private $db; | |
9 | + | |
10 | + public function __construct(){ | |
11 | + try { | |
12 | + $this->db = new PDO("mysql:host=localhost;dbname=study", $this->user, $this->password, $this->option); | |
13 | + }catch (PDOException $e){ | |
14 | + echo "<h2>Database Connection FAIL!!</h2>"; | |
15 | + echo "Because : " . $e . "<hr>"; | |
16 | + } | |
17 | + } | |
18 | + | |
19 | + public function getDB() { | |
20 | + return $this->db; | |
21 | + } | |
22 | +}(No newline at end of file) |
+++ 20220126/Student.php
... | ... | @@ -0,0 +1,28 @@ |
1 | +<?php | |
2 | + | |
3 | +class Student | |
4 | +{ | |
5 | + // 멤버변수 선언 | |
6 | + private $name; | |
7 | + private $age; | |
8 | + private $number; | |
9 | + private $depart; | |
10 | + private $phone; | |
11 | + | |
12 | + // 생성자 (인스턴스 생성 시 최초 1회 실행) | |
13 | + public function __construct($name, $age, $number, $depart, $phone) { | |
14 | + $this -> name = $name; | |
15 | + $this -> age = $age; | |
16 | + $this -> number = $number; | |
17 | + $this -> depart = $depart; | |
18 | + $this -> phone = $phone; | |
19 | + } | |
20 | + | |
21 | + // In PHP 함수 리턴 값 표시 X 가능 | |
22 | + function introduce() { | |
23 | + echo "Hi My name is <strong>" . $this->name . "</strong>.<br>"; | |
24 | + echo "Age is <strong>" . $this->age . "</strong> and depart is <strong>" . $this->depart . "</strong>.<br>"; | |
25 | + echo "Student number is <strong>" . $this->number . "</strong>.<br>"; | |
26 | + echo "Also My Cell Phone number is <strong>" . $this->phone . "</strong>."; | |
27 | + } | |
28 | +}(No newline at end of file) |
--- 20220126/dbtest.php
+++ 20220126/dbtest.php
... | ... | @@ -26,13 +26,19 @@ |
26 | 26 |
echo "AGE : " . $value["age"] . "<br>"; |
27 | 27 |
} |
28 | 28 |
|
29 |
- /* INSERT */ |
|
30 |
-// $stmt = $db -> query("insert into address values(NULL, '임효원', 28, '010-5489-5486', '강원', '1665-12-15')"); |
|
31 |
-// $result = $stmt->execute(); |
|
29 |
+ $name = "안효원"; |
|
30 |
+ $age = 25; |
|
31 |
+ $tel = "010-5635-5918"; |
|
32 |
+ $area = "강원도"; |
|
33 |
+ $birth = "1998-08-07"; |
|
32 | 34 |
|
33 |
- /* UPDATE */ |
|
34 |
- $stmt = $db -> query("update address set name='안효원' where name='황동재'"); |
|
35 |
- $result = $stmt->execute(); |
|
35 |
+ /* Prepare Statement */ |
|
36 |
+ $stmt = $db -> prepare("insert into address values(NULL, ?, ?, ?, ?, ?)"); |
|
37 |
+ $stmt -> execute(array($name, $age, $tel, $area, $birth)); |
|
38 |
+ |
|
39 |
+ |
|
40 |
+ |
|
41 |
+ |
|
36 | 42 |
|
37 | 43 |
|
38 | 44 |
|
+++ 20220126/dbtest2.php
... | ... | @@ -0,0 +1,5 @@ |
1 | +<?php | |
2 | + require_once("DB.php"); | |
3 | + | |
4 | + $db = new DB(); | |
5 | + $db = $db->getDB();(No newline at end of file) |
+++ 20220126/student_test.php
... | ... | @@ -0,0 +1,10 @@ |
1 | +<?php | |
2 | + require_once ("Student.php"); | |
3 | + | |
4 | + $student_dongjae = new Student("황동재", 20, "20201111", "AI소프트웨어학과", "010-1111-1111"); | |
5 | + $student_dongjae->introduce(); | |
6 | + | |
7 | + echo "<hr>"; | |
8 | + | |
9 | + $student_hw = new Student("안효원", 25, "20174355", "융합소프트웨어학과", "010-5635-5918"); | |
10 | + $student_hw->introduce(); |
Add a comment
Delete comment
Once you delete this comment, you won't be able to recover it. Are you sure you want to delete this comment?