Export MySQL database using PHP
To export a MySQL database using PHP, you can use the mysqldump
command-line utility or the mysqli
extension. Here's an example of how to use the mysqli
extension to create a backup of a database:
<?php
// Connect to the database
$conn = mysqli_connect('host', 'username', 'password', 'database');
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Export the database
$sql = "SELECT * INTO OUTFILE '/path/to/backup.sql' FROM database";
if (mysqli_query($conn, $sql)) {
echo "Database backup successfully created!";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
// Close the connection
mysqli_close($conn);
This example will create a backup of the database
in the file /path/to/backup.sql
. You can adjust the path and filename to suit your needs.
Alternatively, you can use the mysqldump
utility to create a backup of the database like this:
<?php
$command = "mysqldump -u username -p password database > /path/to/backup.sql";
system($command);
This will create a backup of the database
in the file /path/to/backup.sql
. Again, you can adjust the path and filename to suit your needs.
Note that both of these examples assume that you have the necessary permissions to create files on the server and that the mysqldump
utility is available on the server.