-
Notifications
You must be signed in to change notification settings - Fork 0
/
backupAllEmps.php
85 lines (76 loc) · 2.42 KB
/
backupAllEmps.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "company";
//this script well run every 5 h
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// calling methods:
// firest chick the Integrity of two taboles
chickIntegrity($conn);
// chick the Integrity of two taboles****************************************************************************
function chickIntegrity($conn){
if(getAllEmps($conn)==getAllEmpCopys($conn)){
print("no new data to backup..");
}else{
backUp($conn);
}
}
//****get All employees and return the number of rows so we can chick it leater for any update in the datebase */
function getAllEmps($conn){
$sql = "SELECT * FROM employee";
$result = $conn->query($sql);
$emp =array ();
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
array_push($emp,$row);
}
} else {
echo "0 results";
}
// if we wont to print the data
// print_r(json_encode($emp));
return $result->num_rows;
}
//*get All employees_copy and return the number of rows so we can chick it leater for any update in the datebase */
function getAllEmpCopys($conn){
$sql = "SELECT * FROM employee_copy";
$result = $conn->query($sql);
$emp =array ();
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
array_push($emp,$row);
}
} else {
echo "0 results";
}
// if we wont to print the data
// print_r(json_encode($emp));
return $result->num_rows;
}
// if we need to print out the number of rows
// print( getAllEmps($conn) );
// print( getAllEmpCopys($conn) );
function backUp($serverConnection){
$lastCopyedID=getAllEmpCopys($serverConnection) + 1;
$curranteLastID=getAllEmps($serverConnection) + 1;
// print($lastCopyedID);
// print($curranteLastID);
for($i=$lastCopyedID; $i<=$curranteLastID; $i++){
$qurey="INSERT INTO employee_copy
SELECT * FROM employee WHERE employee_id = $i";
$result = $serverConnection->query($qurey) ;
print("<br/>employe data with id: ".$i." bcakuped...");
}
//optionally send email contante the number of backuped data
mail("amen.pj@protonmail.com","daly backup","number of backuped rows: ". $curranteLastID - $lastCopyedID);
}
$conn->close();
?>