mysqli::query
(PHP 5, PHP 7, PHP 8)
mysqli::query -- mysqli_query — Performs a query on the database
설명
객체 지향 스타일
public mysqli::query(string $query
, int $result_mode
= MYSQLI_STORE_RESULT
): mysqli_result|bool
절차적 스타일
mysqli_query(mysqli $mysql
, string $query
, int $result_mode
= MYSQLI_STORE_RESULT
): mysqli_result|bool
데이터베이스에 대해 query
를 수행합니다.
non-DML 쿼리(INSERT, UPDATE 또는 DELETE 아님)의 경우 이 함수는 mysqli_real_query() 다음에 mysqli_use_result() 또는 mysqli_store_result()를 호출하는 것과 유사합니다.
메모:
mysqli_query()에 서버의 max_allowed_packet
보다 긴 문장을 전달하는 경우, MySQL Native Driver(mysqlnd
)를 사용하는지, MySQL 클라이언트 라이브러리(libmysqlclient
)를 사용하는지에 따라 반환되는 오류 코드가 다릅니다. 동작은 다음과 같습니다.
- Linux의
mysqlnd
는 1153의 오류 코드를 반환합니다. 오류 메시지는
을 의미합니다.max_allowed_packet
바이트보다 큰 패킷이 있음 - Windows의
mysqlnd
는 오류 코드 2006을 반환합니다. 이 오류 메시지는서버가 사라졌음
을 의미합니다. - 모든 플랫폼의
libmysqlclient
는 오류 코드 2006을 반환합니다. 이 오류 메시지는서버가 사라졌음
을 의미합니다.
매개변수
mysql
- 절차적 스타일 전용: mysqli_connect() 또는 mysqli_init()에 의해 반환된 mysqli 객체
query
- 쿼리 문자열입니다.
경고
보안 경고: SQL 주입
쿼리에 변수 입력이 포함된 경우 매개변수화된 준비된 명령문을 대신 사용해야 합니다. 또는 데이터 형식이 적절해야 하고 모든 문자열은 mysqli_real_escape_string() 함수를 사용하여 이스케이프되어야 합니다.
result_mode
- 결과 모드는 MySQL 서버에서 결과가 반환되는 방식을 나타내는 3가지 상수 중 하나일 수 있습니다.
MYSQLI_STORE_RESULT
(기본값) - 버퍼링된 결과 세트와 함께 mysqli_result 객체를 반환합니다.MYSQLI_USE_RESULT
- 버퍼링되지 않은 결과 세트와 함께 mysqli_result 객체를 반환합니다. 가져오기를 기다리는 보류 중인 레코드가 있는 한 연결 라인은 사용 중이고 모든 후속 호출은Commands out of sync
을 반환합니다. 오류를 방지하려면 서버에서 모든 레코드를 가져와야 하며 그렇지 않으면 mysqli_free_result()를 호출하여 결과 집합을 삭제해야 합니다.MYSQLI_ASYNC
(mysqlnd와 함께 사용 가능) - 쿼리가 비동기적으로 수행되고 결과 집합이 즉시 반환되지 않습니다. 그런 다음 mysqli_poll()을 사용하여 이러한 쿼리에서 결과를 얻습니다.MYSQLI_STORE_RESULT
또는MYSQLI_USE_RESULT
상수와 함께 사용됩니다.
반환 값
실패 시 false
를 반환합니다. SELECT, SHOW, DESCRIBE
또는 EXPLAIN
과 같이 결과 집합을 생성하는 성공적인 쿼리의 경우 mysqli_query()는 mysqli_result 객체를 반환합니다. 다른 성공적인 쿼리의 경우 mysqli_query()는 true
를 반환합니다.
Examples
예제 #1 mysqli::query() 예제
객체 지향 스타일
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* Create table doesn't return a resultset */
$mysqli->query("CREATE TEMPORARY TABLE myCity LIKE City");
printf("Table myCity successfully created.\n");
/* Select queries return a resultset */
$result = $mysqli->query("SELECT Name FROM City LIMIT 10");
printf("Select returned %d rows.\n", $result->num_rows);
/* If we have to retrieve large amount of data we use MYSQLI_USE_RESULT */
$result = $mysqli->query("SELECT * FROM City", MYSQLI_USE_RESULT);
/* Note, that we can't execute any functions which interact with the
server until all records have been fully retrieved or the result
set was closed. All calls will return an 'out of sync' error */
$mysqli->query("SET @a:='this will not work'");
절차적 스타일
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$link = mysqli_connect("localhost", "my_user", "my_password", "world");
/* Create table doesn't return a resultset */
mysqli_query($link, "CREATE TEMPORARY TABLE myCity LIKE City");
printf("Table myCity successfully created.\n");
/* Select queries return a resultset */
$result = mysqli_query($link, "SELECT Name FROM City LIMIT 10");
printf("Select returned %d rows.\n", mysqli_num_rows($result));
/* If we have to retrieve large amount of data we use MYSQLI_USE_RESULT */
$result = mysqli_query($link, "SELECT * FROM City", MYSQLI_USE_RESULT);
/* Note, that we can't execute any functions which interact with the
server until all records have been fully retrieved or the result
set was closed. All calls will return an 'out of sync' error */
mysqli_query($link, "SET @a:='this will not work'");
위의 예는 다음과 유사한 결과를 출력합니다.
Table myCity successfully created. Select returned 10 rows. Fatal error: Uncaught mysqli_sql_exception: Commands out of sync; you can't run this command now in...
기타
- mysqli_real_query() - SQL 쿼리 실행
- mysqli_multi_query() - 데이터베이스에 대해 하나 이상의 쿼리를 수행합니다.
- mysqli_prepare() - 실행을 위해 SQL 문 준비
- mysqli_free_result() - 결과와 관련된 메모리를 해제합니다.