안효원 안효원 2022-01-26
DB Connection add
@e53e323a998686c1c380b266df43d2a6b2ca5715
 
20220126/DB.php (added)
+++ 20220126/DB.php
@@ -0,0 +1,22 @@
+<?php
+
+class DB
+{
+    private $user = "root";
+    private $password = "1234";
+    private $option = array(PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION);
+    private $db;
+
+    public function __construct(){
+        try {
+            $this->db = new PDO("mysql:host=localhost;dbname=study", $this->user, $this->password, $this->option);
+        }catch (PDOException $e){
+            echo "<h2>Database Connection FAIL!!</h2>";
+            echo "Because : " . $e . "<hr>";
+        }
+    }
+
+    public function getDB() {
+        return $this->db;
+    }
+}(No newline at end of file)
 
20220126/Student.php (added)
+++ 20220126/Student.php
@@ -0,0 +1,28 @@
+<?php
+
+class Student
+{
+    // 멤버변수 선언
+    private $name;
+    private $age;
+    private $number;
+    private $depart;
+    private $phone;
+    
+    // 생성자 (인스턴스 생성 시 최초 1회 실행)
+    public function __construct($name, $age, $number, $depart, $phone) {
+        $this -> name = $name;
+        $this -> age = $age;
+        $this -> number = $number;
+        $this -> depart = $depart;
+        $this -> phone = $phone;
+    }
+
+    // In PHP 함수 리턴 값 표시 X 가능
+    function introduce() {
+        echo "Hi My name is <strong>" . $this->name . "</strong>.<br>";
+        echo "Age is <strong>" . $this->age . "</strong> and depart is <strong>" . $this->depart . "</strong>.<br>";
+        echo "Student number is <strong>" . $this->number . "</strong>.<br>";
+        echo "Also My Cell Phone number is <strong>" . $this->phone . "</strong>.";
+    }
+}(No newline at end of file)
20220126/dbtest.php
--- 20220126/dbtest.php
+++ 20220126/dbtest.php
@@ -26,13 +26,19 @@
         echo "AGE : " . $value["age"] . "<br>";
     }
 
-    /* INSERT */
-//    $stmt = $db -> query("insert into address values(NULL, '임효원', 28, '010-5489-5486', '강원', '1665-12-15')");
-//    $result = $stmt->execute();
+    $name = "안효원";
+    $age = 25;
+    $tel = "010-5635-5918";
+    $area = "강원도";
+    $birth = "1998-08-07";
 
-    /* UPDATE */
-    $stmt = $db -> query("update address set name='안효원' where name='황동재'");
-    $result = $stmt->execute();
+    /* Prepare Statement */
+    $stmt = $db -> prepare("insert into address values(NULL, ?, ?, ?, ?, ?)");
+    $stmt -> execute(array($name, $age, $tel, $area, $birth));
+
+
+
+
 
 
 
 
20220126/dbtest2.php (added)
+++ 20220126/dbtest2.php
@@ -0,0 +1,5 @@
+<?php
+    require_once("DB.php");
+
+    $db = new DB();
+    $db = $db->getDB();(No newline at end of file)
 
20220126/student_test.php (added)
+++ 20220126/student_test.php
@@ -0,0 +1,10 @@
+<?php
+    require_once ("Student.php");
+
+    $student_dongjae = new Student("황동재", 20, "20201111", "AI소프트웨어학과", "010-1111-1111");
+    $student_dongjae->introduce();
+
+    echo "<hr>";
+
+    $student_hw = new Student("안효원", 25, "20174355", "융합소프트웨어학과", "010-5635-5918");
+    $student_hw->introduce();
Add a comment
List