![](/img/trans.png)
[英]This query assigns a value to all records in the table, but I want to assign the value only to the last record
[英]I want to backup last records of a table with mysqldump
到目前為止,我有這個:
exec('mysqldump --user=$DBUSER --password=$DBPASSWD --host= localhost DB_NAME (select column from database.table where= column > 1234) > table.sql');
當然,當我使用瀏覽器在服務器上打開文件時,什么也不會發生。 現在,下一個代碼確實輸出了一個SQL文件,但輸出了一個空文件:
set_time_limit(600);
system("mysqldump -h localhost -u $DBUSER -p $DBPASSWD $DATABASE > table.sql");
我需要每天生成的記錄,因此以后可以進行增量備份。
為什么system()
不輸出文件而exec()
不輸出文件?
如果您的目標是對數據庫進行增量備份,那么mysqldump
並不是理想的選擇。
您可以在這里做兩件事:
[BAD WAY]使用您的PHP代碼以CSV和JSON格式序列化並轉儲新記錄,然后使用它。
您可能希望在MySQL中使用binlog可以幫助您做到這一點( 單擊鏈接以閱讀MySQL備份方法 )
傾銷選擇性記錄:
$recset = mysql_query("select column from database.table where= column > 1234");
for($recset as $row){
file_put_contents("filename.json", json_encode($row) . "\n")
}
現在,一旦需要,您可以逐行讀取此文件,並使用json_enocode
將數據恢復為php數組/對象,並隨心所欲地對其進行處理。
[編輯:查看完您的pastebin代碼后]
固定一些變量名和代碼后,即可根據需要工作。
$docsql='nts.sql';
require("cnxn.php");
error_reporting(E_ALL);
ini_set('display_errors', '1');
$q1=mysqli_query($cnx,"SELECT * FROM table WHERE col > '45678' ORDER BY col ASC");
$data_records=array();
while($r1=mysqli_fetch_assoc($q1)){
$cntn=htmlentities($r1['cntn']);
array_push($data_records, "(".$r1['col1'].",'".$r1['col2']."','".$r1['col3']."','".$r1['col4']."','".$r1['col5']."')");
}
$insert="INSERT INTO notas (col1, col2, col3, col4, col5) VALUES ";
file_put_contents($docsql, $insert);
foreach($data_records as $record){
file_put_contents($docsql, $record) . "\n", FILE_APPEND);
}
在akm建議的解決方案中,我添加了最后一個修飾符,以分號代替最后的昏迷:
$docsql='nts.sql';
require("cnxn.php");
error_reporting(E_ALL);
ini_set('display_errors', '1');
$q1=mysqli_query($cnx,"SELECT * FROM table WHERE col > '45678' ORDER BY col ASC");
$data_records=array();
while($r1=mysqli_fetch_assoc($q1)){
$cntn=htmlentities($r1['cntn']);
array_push($data_records, "(".$r1['col1'].",'".$r1['col2']."','".$r1['col3']."','".$r1['col4']."','".$r1['c ol5']."')");
}
$insert="INSERT INTO notas (col1, col2, col3, col4, col5) VALUES ";
file_put_contents($docsql, $insert);
foreach($data_records as $record){
file_put_contents($docsql, $record) . "\n", FILE_APPEND);
}
$chng = fopen("nts.sql", "a+") or die("cant open file");
$rmv = fstat($chng);
ftruncate($chng, $rmv['size']-2);
fwrite($chng,';');
fclose($chng);
那做得很好。 謝謝akm you Rock ...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.