728x90

자주 사용해본 방식이 아니라서 자료 찾으려면 시간도 걸리고 해서 MySQLi 방식(Prepared Statements in MySQLi
)으로 된 예제를 적어둔다.


<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// prepare and bind
$stmt = $conn->prepare("INSERT INTO member (firstname, lastname, email) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $firstname, $lastname, $email);

// set parameters and execute
$firstname = "John";
$lastname = "Doe";
$email = "john@example.com";
$stmt->execute();

$firstname = "Mary";
$lastname = "Moe";
$email = "mary@example.com";
$stmt->execute();

$firstname = "Julie";
$lastname = "Dooley";
$email = "julie@example.com";
$stmt->execute();

echo "New records created successfully";

$stmt->close();
$conn->close();
?>
 


$stmt->bind_result($cnt); // POD 방식에서는 에러 발생
$stmt->close(); //POD 방식에서는 에러 발생

출처 : https://www.w3schools.com/PhP/php_mysql_prepared_statements.asp

Prepared Statements in PDO 과 Prepared Statements in MySQLi

두 방식 예제 모두가 나와 있으므로 참고하면 도움된다.


위 예제로는 부족한 점이 있어서 추가로 예제를  만들어서 테스트를 하고 적어둔다.


테이블 만들기

CREATE TABLE IF NOT EXISTS `NOTICE` (
  `uid` int(11) NOT NULL AUTO_INCREMENT,
  `noticeContent` varchar(100) NOT NULL,
  `noticeName` varchar(20) NOT NULL,
  `noticeDate` datetime NOT NULL,
  PRIMARY KEY (`uid`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1; 


<?php
@extract($_POST); // POST 전송으로 전달받은 값 처리
// $_POST['noticeContent']라고 쓰지 않고, $noticeContent 라고 써도 인식하게 함

// 테스트 목적으로 직접 값을 입력하여 테스트 시도
$noticeContent="공지사항 등록 테스트";
$noticeName="관리자";
$noticeDate=date("Y-m-d H:i:s");

// DB 접속 테스트
$servername = "localhost";
$username = "root";
$password = "autoset";
$dbname = "address";

// Create connection
$con = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($con->connect_error) {
    die("Connection failed: " . $con->connect_error);
}

// DB에 자료가 있는지 검사
$stmt = $con->prepare("SELECT count(*) FROM NOTICE WHERE noticeContent=? and noticeName=?");
$stmt->bind_param("ss", $noticeContent, $noticeName);
$stmt->execute(); // execute query
$stmt->bind_result($cnt); // bind result variables
if($stmt->fetch()){
    //echo $cnt;
    if($cnt == 0){ // 중복 자료가 없으면
        $stmt->close(); // close statement
        $stmt = $con->prepare("INSERT INTO NOTICE(noticeContent, noticeName, noticeDate) VALUES(?, ?, ?)");
        $stmt->bind_param("sss", $noticeContent, $noticeName, $noticeDate);
        $stmt->execute();
        
        $response = array();
        $response["success"] = true;
        echo json_encode($response);
        $stmt->close(); // close statement
    } else {
        $response = array();
        $response["success"] = false;
        echo json_encode($response);
    }
}
$con->close();
?>


types
일치하는 bind 변수의 type을 가리키는 문자열
i : integer
d : double
s : string
b : blob

블로그 이미지

Link2Me

,