justhrun

multiserver.php

Oct 9th, 2014
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.27 KB | None | 0 0
  1. <?php
  2. // file config.php
  3. $server = array(
  4.     array('host'=>'host1', 'user'=>'username1', 'passw'=>'password1', 'db'=>'db_name1'),
  5.     array('host'=>'host2', 'user'=>'username2', 'passw'=>'password2', 'db'=>'db_name2'),
  6.     array('host'=>'host3', 'user'=>'username3', 'passw'=>'password3', 'db'=>'db_name3')
  7. );
  8. ?>
  9.  
  10. <?php
  11. // file task.php
  12. /* bikin dulu table serverlog di semua server, buat log kalo elor
  13. CREATE TABLE IF NOT EXISTS `serverlog` (
  14.   `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  15.   `tanggal` datetime NOT NULL,
  16.   `keterangan` varchar(64) NOT NULL,
  17.   PRIMARY KEY (`id`)
  18. ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
  19. */
  20. include("config.php");
  21. for($i=0;$i<=$count($server);$i++){
  22.     $conn = mysqli_connect($server[$i]['host'], $server[$i]['user'], $server[$i]['passw'], $server[$i]['db']) or die("Error " . mysqli_error($conn));
  23.     $result = mysqli_query($conn,"select * from `table`");
  24.     if($result) {
  25.         $array = mysqli_fetch_array($result);
  26.         $task = $array["field"];
  27.         mysqli_free_result($result);
  28.         mysqli_query($conn,"insert into `table2` set `field2`='".$task."'");
  29.     } else {
  30.         $error = "Error: " . mysqli_error($conn);
  31.         $query = "insert into `serverlog` values (null, NOW(), '$error')";
  32.         mysqli_query($conn,$query);
  33.     }
  34.     mysqli_close($conn);
  35. }
  36. ?>
Advertisement
Add Comment
Please, Sign In to add comment