problem with an Array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
IGGt
Forum Contributor
Posts: 173
Joined: Thu Nov 26, 2009 9:22 am

problem with an Array

Post by IGGt »

I'm sure I've done this before, but can't for the life of me figure it out.
I have an array that contains a list of database's and associated details ($slaveRes_array).
I want to run a MySQL query against this (Show slave status), and then capture the output into a new array ($slaveState_array).

But it doesn't work, I have got:

Code: Select all

$query_slave = "show slave status";

$slaveState_array = array();

for($i = 0; $i <sizeof($slaveRes_array); $i++) {
	$con = mysql_connect($slaveRes_array[$i]['server'], $slaveRes_array[$i]['user'], $slaveRes_array[$i]['password']);
        $result = mysql_query($query_slave, $con);
        $db = $slaveRes_array[$i]['database'];
		while($row = mysql_fetch_assoc($result)) 	{
			foreach ($row as $attribute)
				for($a = 0; $a <sizeof($result); $a++) 	{
					$slaveState_array[] = $result[$a][$attribute] => $attribute;
														}
													} 
        mysql_close($con); }


but it doesn't work. What am I doing wrong.
IGGt
Forum Contributor
Posts: 173
Joined: Thu Nov 26, 2009 9:22 am

Re: problem with an Array

Post by IGGt »

ah, found it.

Code: Select all

$dbs = "dbname";

$query_array = array();
$query_array[] = array(	'query' => "SHOW SLAVE STATUS",
        				'name' => "slaveStatus");

$slaveState_array = array();

for($a = 0; $a <sizeof($slaveRes_array); $a++) {
	$con = mysql_connect($slaveRes_array[$a]['server'], $slaveRes_array[$a]['user'], $slaveRes_array[$a]['password']); 
	mysql_select_db($dbs, $con);
	$dbName = $slaveRes_array[$a]['database'];        
	for($b = 0; $b <sizeof($query_array); $b++) {
		$result = mysql_query($query_array[$b]['query'], $con) or print("<p>ERROR:</p>".mysql_error());
		if ( !$result ) { goto end;}        
		while($row = mysql_fetch_assoc($result)) 	{
			foreach ($row as $attribute)
				$slaveState_array[] = array($query_array[$b]['name'] => $attribute);								
													}
												}
	mysql_close($con); }
end:
Post Reply