Hey.
You just need to execute the same query twice, once for each connection.
You would just need to create a function that allows you to do that.
For example:
(Note, I left out any sort of error checking for clarity.) - <?php
-
class Database
-
{
-
private static $dbLink1;
-
private static $dbLink2;
-
-
private static function connect()
-
{
-
self::$dbLink1 = new mysqli("localhost", "user" ,"pw", "database");
-
self::$dbLink2 = new mysqli("mysql.example.com", "user", "pw", "database");
-
}
-
-
public static function query($sql)
-
{
-
if(is_null(self::$dbLink1) || is_null(self::$dbLink2)) {
-
self::connect();
-
}
-
-
$result1 = self::$dbLink1->query($sql);
-
$result2 = self::$dbLink2->query($sql);
-
-
if(!$result1) {
-
user_error("Failed to execute query on local SQL server.", E_USER_WARNING);
-
}
-
if(!$result2) {
-
user_error("Failed to execute query on remote SQL server.", E_USER_WARNING);
-
}
-
}
-
}
-
?>
Which would allow you to just do:
- Database::query("INSERT INTO whatever(col) VALUES('val1', 'val2')");
And it would be executed on both your servers.
Keep in mind tho that the queries are not executed at the exact same time, so if you are inserting times or dates, it would be advisable to create them use PHP rather then using the MySQL NOW() function. (Or any other such function.)
Buuuut...
It is worth mentioning that MySQL supports automatic replication.
See the documentation on
Replication.
It's a bit more complex, but if you are doing something on a larger scale, it is worth looking into.