php.net 에서 제공한 샘플 코드

<?php
$stmt = $dbh->prepare("INSERT INTO REGISTRY (name, value) VALUES (:name, :value)");
$stmt->bindParam(':name', $name);
$stmt->bindParam(':value', $value);

// insert one row
$name = 'one';
$value = 1;
$stmt->execute();

// insert another row with different values
$name = 'two';
$value = 2;
$stmt->execute();
?>



따라하기(흉내내기)
<?php

class dbMySqli extends mysqli implements dbSwitch
{

    # @ interface : bindParamsResult
    # ("SELECT * FROM `TABLE` WHERE name=':1' and age=':2'",array('php',26));
    # ("INSERT `TABLE` (name,age) VALUES (':a',':b')",array('ajax',27));
    # ("DELETE FROM `TABLE` WHERE name=':A' and age=':B'",array('ax',28));
    public function bindParams($query,$args=array()){
        if(strpos($query,':') !==false){
            preg_match_all("/(\:[a-zA-Z0-9])/s",$query,$matches);
            if(is_array($matches)){
                foreach($matches[0] as $n => $s){
                    $query = str_replace($s,$args[$n],$query);
                }
            }
        }
    return $query;
    }

}


## 사용방법 (영문 대소문자 , 숫자 어떤거든 상관없음 중복되면 안됨)
@숫자
$mysqli->bindParams("SELECT * FROM `TABLE` WHERE name=':1' and age=':2'",array('나당',27));
);

@영문 소문자
$mysqli->bindParams("SELECT * FROM `TABLE` WHERE name=':a' and age=':b'",array('나당',27));
);


@ 영문 대문자
$mysqli->bindParams("SELECT * FROM `TABLE` WHERE name=':1' and age=':2'",array('나당',27));
);


@ 숫자,영문
$mysqli->bindParams("SELECT * FROM `TABLE` WHERE name=':1' and age=':a'",array('나당',27));
);

+ Recent posts