What methods can be used to export data from a MySQL database to a text file in PHP?

To export data from a MySQL database to a text file in PHP, you can use the following steps: 1. Connect to the MySQL database using mysqli or PDO. 2. Execute a SELECT query to retrieve the data you want to export. 3. Loop through the results and write them to a text file using file handling functions like fopen, fwrite, and fclose.

<?php
// Connect to MySQL database
$pdo = new PDO('mysql:host=localhost;dbname=database_name', 'username', 'password');

// Select data from database
$stmt = $pdo->query("SELECT * FROM table_name");

// Open a text file for writing
$file = fopen('exported_data.txt', 'w');

// Loop through results and write to text file
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    fwrite($file, implode("\t", $row) . "\n");
}

// Close the text file and database connection
fclose($file);
$pdo = null;
?>