How can PHP developers effectively handle and transfer database files for sharing or backup purposes?
To effectively handle and transfer database files for sharing or backup purposes, PHP developers can use the built-in functions for exporting and importing database files. One common approach is to use the mysqldump command to export the database as a SQL file, which can then be transferred or shared easily. To import the database file, developers can use the mysql command to restore the database from the SQL file.
// Export database as SQL file
$host = 'localhost';
$user = 'username';
$pass = 'password';
$db_name = 'database_name';
$output_file = 'backup.sql';
exec("mysqldump -h $host -u $user -p$pass $db_name > $output_file");
// Import database from SQL file
$host = 'localhost';
$user = 'username';
$pass = 'password';
$db_name = 'database_name';
$input_file = 'backup.sql';
exec("mysql -h $host -u $user -p$pass $db_name < $input_file");