我想创建一个脚本,为数据库的每个表创建一个备份。现在我有了这个:
连接数据库:
<?php
$AdresaBD="localhost";
$UtilizatorBD="root";
$NumeBD="auth";
$NumeBD1="auth1";
$ParolaBD="";
$con1=mysqli_connect($AdresaBD,$UtilizatorBD,$ParolaBD,$NumeBD);
$con2=mysqli_connect($AdresaBD,$UtilizatorBD,$ParolaBD,$NumeBD1);
if( mysqli_connect_errno() ) {
echo 'Nu ma pot conecta la baza de date!'.mysqli_connect_errno();
}
?>
剧本:
<?php
//auth
$backup1 = fopen('auth_back_'.date('j-M-Y').'.sql','w+');
$CerereSQL = "SELECT * INTO OUTFILE '$backup1' FROM `angajati`,`concedii`";
$result = mysqli_query($con1,$CerereSQL);
//auth1
$backup2 = fopen('auth1_back_'.date('j-M-Y').'.sql','w+');
$CerereSQL = "SELECT * INTO OUTFILE '$backup2' FROM `contact`,`persoane_active`";
$result = mysqli_query($con2,$CerereSQL);
?>
(例如,我将选择一个数据库来解释)我有表angajati,在我的数据库auth中的concedii,这个脚本应该用这些表创建一个备份文件sql,当我测试文件,看看他们是否真的有它们里面的数据,换句话说,当我尝试导入它们时,它表示成功,但数据没有显示出来。那是因为文件是空白的,无论我做什么。
答案 0 :(得分:0)
尝试将代码分解为更简单的版本,然后解决问题。
我认为您尝试备份的文件存在问题:
原始代码:
$backup1 = fopen('auth_back_'.date('j-M-Y').'.sql','w+');
$CerereSQL = "SELECT * INTO OUTFILE '$backup1' FROM `angajati`,`concedii`";
$result = mysqli_query($con1,$CerereSQL);
简化代码:
$backupFile1 = 'auth_back.sql';
$CerereSQL = "SELECT * INTO OUTFILE '$backupFile1' FROM angajati";
if(mysqli_query($con1,$CerereSQL)) {
echo 'Database backed up';
}
else {
echo 'There was an error backing that database up';
}