![](/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.