Page 1 of 1

Not Adding to an Array

Posted: Wed Jan 26, 2011 5:06 am
by IGGt
I'm sure I am just missing something obvious, but I can't see what. My Script checks Multiple Databases, and runs a selection of MySQL queries against each one., and then writes it to an array. But for some reason, it always seems to overwrite itself so it ends up having data from the last database only.

Code: Select all

//$mf_array = Array containing database's and their connection details
//$RMquery_array = Array containining MySQL queries
$RMa = array();	 //new array to be created					
						
foreach($mf_array as $rm_array) {
		
	$con = mysql_connect($rm_array['server'], $rm_array['user'], $rm_array['password']);
		mysql_select_db($dbs, $con);
      
	        for($a = 0; $a <sizeof($mf_array); $a++) { 	
		        $RMa[$a]['db'] = $mf_array[$a]['database'];  // This bit works fine, adding the correct database names to the new Array
	        		        
// QUERY 1    							
        	$RMres = mysql_query($RMquery_array[0]['query1']) ;
        		if ( !$RMres ) { 	$error = mysql_error();
	      			print "<table ><tr><th>ERROR HAS OCCURRED</th></tr>\"";
        			print "<tr><td>".$error."</td></tr></table>";
    					}              
                while($RMrow = mysql_fetch_assoc($RMres)) {
	                $instance[$rm_array['database']] = $RMrow;
	                $RMa[$a]['allowRun'] = $RMrow['allowRun'];
	                $RMa[$a]['whenSet'] = $RMrow['whenSet'];
	                	
	               						}
	               			
// QUERY 2							
        	$RMres2 = mysql_query($RMquery_array[0]['query2']) ;
        		if ( !$RMres2 ) { 	$error = mysql_error();
	      			print "<table><tr><th>ERROR HAS OCCURRED</th></tr>\"";
        			print "<tr><td>".$error."</td></tr></table>";
    					}              
                while($RMrow2 = mysql_fetch_assoc($RMres2)) {
	                $instance2[$rm_array['database']] = $RMrow2;
	                $RMa[$a]['id'] = $RMrow2['id'];
	                $RMa[$a]['now_running'] = $RMrow2['now_running'];
	                $RMa[$a]['dt'] = $RMrow2['dt'];
	               						}               						
        						}
	mysql_close($con);
};
This gives me the following Array

Array
(
[0] => Array
(
[db] => MySQL06
[allowRun] => 0
[whenSet] => 2011-01-26 09:43:00
[id] => 14
[now_running] => Processing completed
[dt] => 2011-01-24 15:54:23
)

[1] => Array
(
[db] => MySQL07
[allowRun] => 0
[whenSet] => 2011-01-26 09:43:00
[id] => 14
[now_running] => Processing completed
[dt] => 2011-01-24 15:54:23
)

)


As you can see, apart form the first element (db), everything else is the same, but the first one should look like:
[0] => Array
(
[db] => MySQL06
[allowRun] => 1
[whenSet] => 2011-01-26 09:42:00
[id] => 17
[now_running] => Processing completed
[dt] => 2011-01-24 15:50:26
)

Re: Not Adding to an Array

Posted: Wed Jan 26, 2011 5:41 am
by IGGt
I think I have fixed it by changing:

Code: Select all

foreach($mf_array as $rm_array) {
               
        $con = mysql_connect($rm_array['server'], $rm_array['user'], $rm_array['password']);
                mysql_select_db($dbs, $con);
     
                for($a = 0; $a <sizeof($mf_array); $a++) {
to

Code: Select all


for($a = 0; $a <sizeof($mf_array); $a++) {						
		
	$con = mysql_connect($mf_array[$a]['server'], $mf_array[$a]['user'], $mf_array[$a]['password']);
		mysql_select_db($dbs, $con);
Basically removing some of the foreach statements, and just using the for($a .....) statement