안효원 안효원 2022-01-27
middle
@adfb5fc694f5ddd01f71debd8daf80f3a8907fb5
20220126/DB.php
--- 20220126/DB.php
+++ 20220126/DB.php
@@ -10,6 +10,7 @@
     public function __construct(){
         try {
             $this->db = new PDO("mysql:host=localhost;dbname=study", $this->user, $this->password, $this->option);
+            $this->db->exec("set names UTF8");
         }catch (PDOException $e){
             echo "<h2>Database Connection FAIL!!</h2>";
             echo "Because : " . $e . "<hr>";
@@ -19,4 +20,53 @@
     public function getDB() {
         return $this->db;
     }
-}
(No newline at end of file)
+
+    public function selectTest() {
+        try {
+            $stmt = $this->db->prepare("select * from address where name = ? limit 1");
+            $stmt->execute(array("안효원"));
+            $result = $stmt->fetchAll();
+            return $result;
+        } catch (PDOException $e){
+            return $e;
+        }
+    }
+
+    public function insertTest($name, $age, $tel, $area, $birth) {
+        try {
+            $stmt = $this->db->prepare("insert into address values(NULL, ?, ?, ?, ?, ?)");
+            $stmt->execute(array($name, $age, $tel, $area, $birth));
+            return true;
+        }catch (PDOException $e) {
+            echo $e;
+            return false;
+        }
+    }
+
+    public function updateTest($name) {
+        try {
+            $stmt = $this->db->prepare("update address set name=? where name='안효원'");
+            $stmt->execute(array($name));
+            return true;
+        }catch (PDOException $e) {
+            echo $e;
+            return false;
+        }
+    }
+
+    public function deleteTest($name) {
+        try {
+            $stmt = $this->db->prepare("delete from address where name=?");
+            $stmt->execute(array($name));
+            return true;
+        }catch (PDOException $e){
+            echo $e;
+            return false;
+        }
+    }
+
+}
+
+
+
+
20220126/dbtest2.php
--- 20220126/dbtest2.php
+++ 20220126/dbtest2.php
@@ -2,4 +2,43 @@
     require_once("DB.php");
 
     $db = new DB();
-    $db = $db->getDB();
(No newline at end of file)
+
+    echo "Select Test <br>";
+    $list = $db->selectTest();
+
+    print_r($list);
+
+
+    echo "<br><hr>Insert Test<br>";
+    $result = $db->insertTest("안효원", 26, "010-5635-5918", "강원", "1998-08-07");
+    if($result) {
+        echo "Insert Success!";
+    }else {
+        echo "Insert Fail!";
+    }
+
+
+    echo "<br><hr>Update Test<br>";
+    $result = $db->updateTest("김현식");
+    if($result) {
+        echo "update Success!";
+    }else {
+        echo "update Fail!";
+    }
+
+
+    echo "<br><hr>Delete Test<br>";
+    $result = $db->deleteTest("안효원");
+    if($result) {
+        echo "delete Success!";
+    }else {
+        echo "delete Fail!";
+    }
+
+
+
+
+
+
+
+
Add a comment
List