# \[DB] 사용 가이드

### DB Prepare Statement 바인딩 가이드

사용 예시

```php
// db 모듈 호출
$db = \App::getInstance('DB');

// bind 할 배열 생성
$arrBind = [];

// prepare statement query 생성
$query = "SELECT COUNT(*) as cnt FROM "
    . CUSTOM_TABLE 
    . " WHERE sno = ? AND customColumn = ?";

// 데이터 바인딩
// 첫 번째 ?에 $customSno가 바인딩됩니다 (정수)
$db->bind_param_push($arrBind, 'i', $customSno);
// 두 번째 ?에 $customColumn가 바인딩됩니다 (문자열)
$db->bind_param_push($arrBind, 's', $customColumn);
// 쿼리 실행
$result = $db->query_fetch($query, $arrBind, false);
unset($arrBind);

return $result['cnt'];
```

DB Prepare Statement 바인딩 사용 방법

* 쿼리문 내에서 바인딩이 필요한 값은 `?`로 표시해 주시기 바랍니다.
* 각 ?에는 bind\_param\_push로 추가한 값이 입력 순서대로 차례로 바인딩됩니다.
  * 예를 들어, 첫 번째 bind\_param\_push의 값은 쿼리의 첫 번째 ?에, 두 번째 값은 두 번째 ?에 바인딩됩니다.
* 파라미터는 `$db->bind_param_push($arrBind, '타입', $값);` 형태로 바인딩 배열에 추가해 주시면 됩니다.
* 쿼리 실행 시에는 `$db->query_fetch($query, $arrBind, false);`와 같이 바인딩 배열을 함께 전달해 주세요.
* 바인딩 타입은 아래 표를 참고하여 데이터 타입에 맞게 지정해 주시기 바랍니다.
* 이 방식을 사용하시면 SQL 인젝션을 효과적으로 방지할 수 있으며, 코드의 가독성과 유지보수성도 높아집니다.

#### 바인딩 타입 표

| 타입 | 설명                      |
| -- | ----------------------- |
| i  | int(정수)                 |
| d  | float(실수)               |
| s  | string(문자열)             |
| b  | blob(이진 데이터, 패킷 단위로 전송) |


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://devcenter-help.nhn-commerce.com/other-guide/database.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
