problem returning array in correct format
Posted: Tue Nov 02, 2010 10:31 am
I have a script that runs a query against a MySQL database, then, if it returns a resultset, it takes the 'freindly name from an array containg the database connections, then adds the data for the resultset.
Unfortunately at the minute, it doesn't quite work the way that I want.
So far I have:
The problem is that at the minute it puts each field into a separate part of the array e.g.
[text]Array (
[0] => Array ( [name] => MySQL02_5083 )
[1] => Array ( [Slave_IO_State] => Waiting for master to send event )
[2] => Array ( [Master_Host] => localhost )
[3] => Array ( [Master_User] => root )[/text]
Whereas what I am trying to achieve is more like:
[text]Array (
[0]=> Array ( [name] => MySQL02 )
[Slave_IO_State] => Waiting for master to send event )
[Master_Host] => localhost )
[Master_User] => root )...
[1] => Array ( [name] => MySQL03[/text]
etc. etc.
But I can't see how to achieve this?
Unfortunately at the minute, it doesn't quite work the way that I want.
So far I have:
Code: Select all
//get list of servers to query, and choose them one at a time
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);
//get list of MySQL Queries, and run them against the current server, one at a time
for($b = 0; $b <sizeof($query_array); $b++) {
$SlaveState = mysql_query($query_array[$b]['query1'], $con);
// 1st Query
while($row = mysql_fetch_assoc($SlaveState)) {
for($c = 0; $c <mysql_num_rows($SlaveState); $c++) {
$slave_array[]['name'] = $slaveRes_array[$a]['database'];
for($d = 0; $d <mysql_num_fields($SlaveState); $d++) {
$slave_array[][mysql_field_name($SlaveState,$d)] = mysql_result($SlaveState,$c,mysql_field_name($SlaveState,$d));
}
}
}
// Run Query2...Query3....etc.
}
The problem is that at the minute it puts each field into a separate part of the array e.g.
[text]Array (
[0] => Array ( [name] => MySQL02_5083 )
[1] => Array ( [Slave_IO_State] => Waiting for master to send event )
[2] => Array ( [Master_Host] => localhost )
[3] => Array ( [Master_User] => root )[/text]
Whereas what I am trying to achieve is more like:
[text]Array (
[0]=> Array ( [name] => MySQL02 )
[Slave_IO_State] => Waiting for master to send event )
[Master_Host] => localhost )
[Master_User] => root )...
[1] => Array ( [name] => MySQL03[/text]
etc. etc.
But I can't see how to achieve this?