What are the advantages of using "INTO OUTFILE" in a SQL query for exporting data compared to other methods?

When exporting data from a SQL query, using "INTO OUTFILE" in the query has several advantages compared to other methods. It allows for direct exporting of query results to a file on the server, which can be more efficient than fetching results and then writing them to a file programmatically. Additionally, it can handle large datasets more efficiently and can be a simpler solution for exporting data without the need for additional programming logic.

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

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

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

// SQL query to export data into a CSV file
$sql = "SELECT * INTO OUTFILE '/path/to/file.csv' FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n' FROM table_name";

if ($conn->query($sql) === TRUE) {
  echo "Data exported successfully";
} else {
  echo "Error exporting data: " . $conn->error;
}

$conn->close();
?>