Reputation: 1045
i have a db (let's call it A
)
i need the informations to be in multiple DB's (B, C, D needs EXACTLY the same informations)
can i make something that will auto-update all of them every (X) hours?
#i need this in order to sync 2 joomla components using IIS 7 / php.
Tryed (works nice the first time, but then if one is changed it goes fked up because there's no Primary in this component):
#Getting my data
$mysqli = @new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_WORLD);
/* Vérification de la connexion */
if (mysqli_connect_errno()) {
die("&error=".mysqli_connect_error()."&");
exit();
}
$query = " SELECT table_name
FROM information_schema.tables
WHERE table_name LIKE '%XXXXXXX%'";
$arrTableList = array();
$allInArray = array();
if ($result = $mysqli->query($query)) {
/* Récupère un tableau d'objets */
while ($obj = $result->fetch_object()) {
$arrTableList[] = $obj->table_name;
}
/* free result set */
$result->close();
}
foreach($arrTableList as $key => $value){
$query = " SELECT *
FROM $value";
if ($result = $mysqli->query($query)) {
/* Récupère un tableau d'objets */
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
$allInArray[$value][] = $row;
}
/* free result set */
$result->close();
}
}
the insertion function
function toSwich($newDb, $tmpTag){
global $mysqli;
global $allInArray;
global $tag1;
$mysqli->select_db($newDb);
foreach($allInArray as $table => $tableArrayData){
$table = str_replace ($tag1, $tmpTag, $table);
foreach($tableArrayData as $uselessKey => $valueArray){
$keyList = "";
$valueList = "";
$query = "SELECT COUNT(*) FROM $table WHERE ";
foreach($valueArray as $key => $value){
$key = $mysqli->real_escape_string($key);
$value = $mysqli->real_escape_string($value);
$keyList .= $key . ",";
$valueList .= "'" . $value . "',";
$query .= " $key = '$value' AND";
}
$query = substr($query,0,-3);
$valueList = substr($valueList,0,-1);
$keyList = substr($keyList,0,-1);
if(!$result = $mysqli->query($query)){
print_r($mysqli->error);
echo "<br />";
print_r($query);
echo "<br />";
exit();
}
$row = $result->fetch_row();
if((int)$row[0] == 0){
if(!$mysqli->real_query("INSERT INTO $table ($keyList) VALUES ($valueList)")){
print_r($mysqli->error);
exit();
}
}
}
}
}
Upvotes: 1
Views: 103
Reputation: 473
Configure a Master-Slave replication schema, I'll keep the databases synced constantly
Here its an example how to configure replication: 3 Jump Steps to Configure Master Slave Replication in Mysql
Upvotes: 1