PHP mysqli multi_query() 함수
예 - 객체 지향 스타일
데이터베이스에 대해 여러 쿼리를 수행합니다.
<?php
$mysqli = new mysqli("localhost","my_user","my_password","my_db");
if ($mysqli -> connect_errno) {
echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
exit();
}
$sql = "SELECT Lastname FROM Persons ORDER BY LastName;";
$sql .= "SELECT Country FROM Customers";
// Execute multi query
if ($mysqli
-> multi_query($sql)) {
do {
// Store first result set
if ($result =
$mysqli -> store_result()) {
while ($row =
$result -> fetch_row()) {
printf("%s\n", $row[0]);
}
$result
-> free_result();
}
// if there are more result-sets, the print a
divider
if ($mysqli -> more_results()) {
printf("-------------\n");
}
//Prepare next result set
} while ($mysqli
-> next_result());
}
$mysqli -> close();
?>
하단의 절차적 스타일의 예를 보십시오.
정의 및 사용
multi_query() / mysqli_multi_query() 함수는 데이터베이스에 대해 하나 이상의 쿼리를 수행합니다. 쿼리는 세미콜론으로 구분됩니다.
통사론
객체 지향 스타일:
$mysqli ->
multi_query(query)
절차 스타일:
mysqli_multi_query(connection, query)
매개변수 값
Parameter | Description |
---|---|
connection | Required. Specifies the MySQL connection to use |
query | Required. Specifies one or more queries, separated with semicolon |
기술적 세부 사항
반환 값: | 첫 번째 쿼리가 실패하면 FALSE |
---|---|
PHP 버전: | 5+ |
예 - 절차 스타일
데이터베이스에 대해 여러 쿼리를 수행합니다.
<?php
$con = mysqli_connect("localhost","my_user","my_password","my_db");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit();
}
$sql = "SELECT Lastname FROM Persons ORDER BY LastName;";
$sql .= "SELECT Country FROM Customers";
// Execute multi query
if (mysqli_multi_query($con, $sql)) {
do {
// Store first result set
if ($result = mysqli_store_result($con)) {
while ($row = mysqli_fetch_row($result)) {
printf("%s\n", $row[0]);
}
mysqli_free_result($result);
}
// if there are more result-sets, the print a
divider
if (mysqli_more_results($con)) {
printf("-------------\n");
}
//Prepare next result set
} while (mysqli_next_result($con));
}
mysqli_close($con);
?>
❮ PHP MySQLi 참조