[英]Export data from database to sql file with PHP
我需要將數據庫中的數據(僅數據,而不是表或數據庫)導出到SQL
文件並保存。 如何編寫一個做到這一點的PHP函數? 我想在運行特定查詢(例如更新博客文章)時在特定情況下調用此函數。
就像是:
/*
* Get the data from the database (already specified) and export it to a SQL file
*
* @param string $dir Where to store the SQL file. Must be a full path.
* @return void
*/
function export_database_data(string $dir) {
// some code that gets the data and store it in $data
// don't know what to do here
// create/update the file
file_put_contents("$dir/data_backup.sql", $data);
}
為什么這個?
(您無需閱讀以下內容即可幫助我解決問題。我將其包含在內,因為我確定如果不包含該內容,則會有人提出來。)
之所以這樣做,是因為我想要備份數據(僅備份數據;表無關緊要,因為我已經知道如何處理備份數據)。
我這樣做是因為我將使用PHP函數exec
來運行git
命令並在SQL
文件中提交更改。 最終目標是跟蹤數據庫中的數據,該數據不會經常更新。 我需要SQL
文件中的數據,我的目標是使用git
因為它易於訪問和與他人協作(我認為沒有更好的解決方案,並且創建自己的跟蹤系統將需要大量的工作和時間)。
您可以使用PHP exec
函數運行mysqldump
命令以僅導出數據。 例如:
exec('mysqldump -u [user] -p[pass] --no-create-info mydb > mydb.sql');
更多信息:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.