'2017/07/29'에 해당되는 글 1건

728x90

PDO 방식으로 MySQL 과 연동하여 데이터를 가져오는 걸 테스트했다.

사용자 정의 함수를 만들어서 연동하는 방법이다.

사용자 함수 CustomFunction($ItemName,$Quantity) 의 결과가 있을 경우에는 데이터를 반환하고, 없으면 false 로 결과 반환을 한다.

true, fasle 로 처리했으므로 if($result = CustomFunction($ItemName,$Quantity)) 문으로 처리하면 원하는 결과를 얻을 수 있다.


<?php
require_once 'pdoconn.php';
// SELECT
$ItemName ='수박';
$Quantity = 500;
if($result = CustomFunction($ItemName,$Quantity)){
    // Fetch 모드를 설정 및 1 row 씩 가져오기
    while($row= $result->fetch(PDO::FETCH_BOTH)) {
        echo $row[0].' | ';
        echo $row[1].' | ';
        echo $row[2].' | ';
        echo $row[3].'<br />';
    }
}
$db = null;

function CustomFunction($ItemName,$Quantity){
    global $db;
    $sql='select uid,ItemName,Price,Quantity from table_items where ItemName=:ItemName and Quantity=:Quantity';
    $stmt = $db->prepare($sql);
    $stmt->bindParam(':ItemName', $ItemName);
    $stmt->bindParam(':Quantity', $Quantity);
    $stmt->execute();
    if($stmt->fetch(PDO::FETCH_BOTH)){
        return $stmt;
    } else {
        return false;
    }
}
?>


함수의 개수가 많아지면 함수명이 중복될 수도 있고 관리가 어려워질 수 있다.

이제 Class 화를 해보자.

<?php
class TestClass
{
    function CustomFunction($ItemName,$Quantity){
        global $db;
        $sql='select uid,ItemName,Price,Quantity from table_items where ItemName=:ItemName and Quantity=:Quantity';
        $stmt = $db->prepare($sql);
        $stmt->bindParam(':ItemName', $ItemName);
        $stmt->bindParam(':Quantity', $Quantity);
        $stmt->execute();
        if($stmt->fetch(PDO::FETCH_BOTH)){
            return $stmt;
        } else {
            return false;
        }
    }
}
?> 

bind_param 은 mysqli_stmt 방식이고, bindParam은 PDOStatement 방식이다.


TestClass 에 있는 함수를 이용하는 예제다.

처음에 연동했던 방식과 비교해서 어떤 부분이 달라졌는지만 살펴보면 알 수 있다.

<?php
require_once 'pdoconn.php';
require_once 'pdoClass.php';
$test = new TestClass;

$ItemName ='수박';
$Quantity = 500;
if($result = $test->CustomFunction($ItemName,$Quantity)){
    // Fetch 모드를 설정 및 1 row 씩 가져오기
    while($row= $result->fetch(PDO::FETCH_BOTH)) {
        echo $row[0].' | ';
        echo $row[1].' | ';
        echo $row[2].' | ';
        echo $row[3].'<br />';
    }
}
$db = null;
?> 


위 방식에는 문제가 있더라.

결과 개수가 다르게 나온다.

제대로 처리하기 위해서는 ....

<?php
include_once 'dbinfo.php';
try{
    // MySQL PDO 객체 생성
    $db = new PDO('mysql:host='.$db['host'].';dbname='.$db['name'].';charset=utf8', $db['user'], $db['pass']);
    // 에러 출력
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(Exception $e) {
    echo 'Failed to obtain database handle : '.$e->getMessage();
}
?>

<?php
class TestClass
{
    function CustomFunction($ItemName,$Quantity){
        global $db;
        $sql='select uid,ItemName,Price,Quantity from table_items where ItemName=:ItemName and Quantity=:Quantity';
        $stmt = $db->prepare($sql);
        $stmt->bindParam(':ItemName', $ItemName);
        $stmt->bindParam(':Quantity', $Quantity);
        $stmt->execute();
        if($this->CustomFunctionCount($ItemName,$Quantity)>0){
            return $stmt;
        } else {
            return false;
        }
    }

    function CustomFunctionCount($ItemName,$Quantity){
        global $db;
        $sql='select count(*) from table_items where ItemName=:ItemName and Quantity=:Quantity';
        $stmt = $db->prepare($sql);
        $stmt->bindParam(':ItemName', $ItemName);
        $stmt->bindParam(':Quantity', $Quantity);
        $stmt->execute();
        return $stmt->fetchColumn();
    }

}
?>

<?php
require_once 'pdoconn.php';
require_once 'pdoClass.php';
$test = new TestClass;

$ItemName ='수박';
$Quantity = 500;

//echo $test->CustomFunctionCount($ItemName,$Quantity).'<br />';
if($result = $test->CustomFunction($ItemName,$Quantity)){
    // Fetch 모드를 설정 및 1 row 씩 가져오기
    while($row= $result->fetch(PDO::FETCH_BOTH)) {
        echo $row[0].' | ';
        echo $row[1].' | ';
        echo $row[2].' | ';
        echo $row[3].'<br />';
    }
}
$db = null;
?>


'Web 프로그램 > PDO' 카테고리의 다른 글

[PHP] PDO MemberClass  (0) 2017.11.16
[PHP] PDO DB 연결방법  (0) 2017.11.16
MySQL 접속방식 PDO 접속방식으로 변환  (0) 2017.07.28
PDO(PHP Data Objects) 개념 및 사용법  (0) 2017.07.25
[리눅스] PDO-MYSQL 모듈 설치  (0) 2017.06.12
블로그 이미지

Link2Me

,