Mysql
 sql >> Teknologi Basis Data >  >> RDS >> Mysql

Perulangan melalui database mysql

Beberapa kode kasar. Tidak di-debug atau diuji...

$masterDBHost = 'localhost';
$masterDBUser = 'username';
$masterDBPass = 'somethingSecret';
$masterDBName = 'theDBname';

$sqlToPerformOnEachDatabases = 'SELECT 1';

// Connect to the Master Database
if( !( $master = mysql_connect( $masterDBHost , $masterDBUser , $masterDBPass ) ) )
  die( 'MySQL Error - Cannot Connect to Master Server' );
if( !mysql_select_db( $masterDBName , $master ) )
  die( 'MySQL Error - Cannot Connect to Master Database' );

// Get the Other Databases to Connect to
$databases = mysql_query( 'SELECT * FROM `databaseTable`' , $master );

// Check your Results
if( !$databases || mysql_num_rows( $databases )==0 ){
  # Nothing to work with
  echo 'Unable to find Databases to Access';
}else{
  # Something to work with
  while( $d = mysql_fetch_assoc( $databases ) ){
    // Connect to the Client Server
    if( !( $temp = mysql_connect( $d['host'] , $d['user'] , $d['pass'] ) ) ){
      # Can't connect to the Server
      echo 'MySQL Error - Failed to connect to '.$d['host'].' as '.$d['user'];
    }elseif( !mysql_select_db( $d['base'] , $temp ) ){
      # Can't connect to the Database
      echo 'MySQL Error - Failed to connect to '.$d['base'].' on '.$d['host'].' as '.$d['user'];
    }elseif( !mysql_query( $sqlToPerformOnEachDatabases , $temp ) ){
      # Your Command, well, stuffed up
      echo 'MySQL Error - Your Command Stuffed Up';
    }else{
      # Your Command worked OK
      echo 'All Good!';
    }
    # Close the connection (probably not needed, but nice to do)
    @mysql_close( $temp );
  }
}

Versi 2

Memungkinkan untuk menjaga koneksi jika Host/Pengguna/Pass yang sama digunakan

Sekali lagi, tidak di-debug atau diuji.

$masterDBHost = 'localhost';
$masterDBUser = 'username';
$masterDBPass = 'somethingSecret';
$masterDBName = 'theDBname';

$sqlToPerformOnEachDatabases = 'SELECT 1';

// Connect to the Master Database
if( !( $master = mysql_connect( $masterDBHost , $masterDBUser , $masterDBPass ) ) )
  die( 'MySQL Error - Cannot Connect to Master Server' );
if( !mysql_select_db( $masterDBName , $master ) )
  die( 'MySQL Error - Cannot Connect to Master Database' );

// Get the Other Databases to Connect to
$databases = mysql_query( 'SELECT * FROM `databaseTable`' , $master );

// Check your Results
if( !$databases || mysql_num_rows( $databases )==0 ){
  # Nothing to work with
  echo 'Unable to find Databases to Access';
}else{
  # Something to work with
  // A variable for the MySQL Connection
  $temp = false;
  // Declare some short-term memory
  $last = array();
  while( $d = mysql_fetch_assoc( $databases ) ){
    // Check Last Loop's details
    if( $temp
        && $last
        && array_diff( $last , $d ) ){
      // New Host, User or Pass
      @mysql_close( $temp );
      $last = false;
    }
    // Connect to the Client Server
    if( !$last
        && !( $temp = mysql_connect( $d['host'] , $d['user'] , $d['pass'] ) ) ){
      # Can't connect to the Server
      echo 'MySQL Error - Failed to connect to '.$d['host'].' as '.$d['user'];
    }elseif( !mysql_select_db( $d['base'] , $temp ) ){
      # Can't connect to the Database
      echo 'MySQL Error - Failed to connect to '.$d['base'].' on '.$d['host'].' as '.$d['user'];
    }elseif( !mysql_query( $sqlToPerformOnEachDatabases , $temp ) ){
      # Your Command, well, stuffed up
      echo 'MySQL Error - Your Command Stuffed Up';
    }else{
      # Your Command worked OK
      echo 'All Good!';
    }
    # Remember this Loop's details
    $last = $d;
  }
  @mysql_close( $temp );
}


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. tidak dapat menampilkan simbol merek dagang di mysql ke html

  2. Ekspor Kamus Data database saya menggunakan MySQL Workbench CE?

  3. MYSQL - Pilih hanya jika baris di LEFT JOIN tidak ada

  4. Java terhubung ke Cloud SQL 2nd Gen dari Appengine Managed VM

  5. Apakah ada cara yang lebih baik untuk mendapatkan data dari dua tabel sekaligus dengan Sphinx/MySQL?