0

I'm trying to figure out how to code a loop in a PHP script that:

  1. gets $hostname, $username $password and $platform from an included script for a bunch of MySQL databases (different remote servers with different access credentials) I just have read-only access to
  2. runs the PHP script on each of the databases

I have all the variables stored in an array that looks like this:

$servers = array(
    'server1' => array(
    'hostname' => '<serverurl>'
    'username' => 'readonly',
    'password' => 'pword',
    'platform' => 'platform'
),
'server2' => array(
    'hostname' => 'serverurl'
    'username' => 'readonly',
    'password' => 'pword',
    'platform' => 'platform'
),
},

I'm having trouble figuring out how to pass those values into a loop statement in my PHP script though - how would I make it run on every server in the array?:

$dbhandle = mysql_connect($hostname, $username, $password) or die(mysql_error('Unable to connect to MySQL'));
echo 'Connected to MySQL<br>';
mysql_select_db($platform, $dbhandle) or die(mysql_error('Unable to connect to database'));
echo 'Connected to database<br>';

Sorry for the noob question/if this is a repeat - I couldn't find anything similar when I searched. Is there a good site for me to look up this sort of thing? Thanks!

1 Answer 1

1
<?php

$dbhandles = array();

foreach($servers as $server => $details) {
  $dbhandles[$server] = mysql_connect($details['hostname'], $details['username'], $details['password']) or die(mysql_error('Unable to connect to MySQL'));
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.