At the moment I have a large number of php files, each one with an array statement in the format:
$array1 = array ( array ( 'Husband' => 'bob',
'Wife' => 'ann' ),
array ( 'Husband' => 'fred',
'Wife' => 'donna' ),
array ( 'Husband' => 'john',
'Wife' => 'mary' ) );
This of course returns from print_r ($array1)
the output (A):
- Array ( [0] => Array ( [Husband] => bob [Wife] => ann ) [1] => Array ( [Husband] => fred [Wife] => donna ) [2] => Array ( [Husband] => john [Wife] => mary ) )
In a separate class file, I access $array1 data, using statements like:
for ($col = 0; $col < sizeof($array1); $col++)
{
echo $array[$col]['Wife']."<br />";
}
On the theory that a large number of anything is a database, I am wanting to migrate the data in the multiple php array files array into a MySQL database, and access the MySQL data from my class file. So I set up the following in MySQL:
CREATE TABLE IF NOT EXISTS `arrays` (
`id` int(5) NOT NULL auto_increment,
`husband` varchar(255) collate utf8_unicode_ci NOT NULL,
`wife` varchar(255) collate utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
INSERT INTO `arrays` (`id`, `husband`, `wife`) VALUES
(1, 'bob', 'ann'),
(2, 'fred', 'donna'),
(3, 'john', 'mary');
Then I access this MySQL database from PHP:
$sql = "SELECT * FROM `database`.`arrays`";
$link = mysql_connect ('localhost', 'username', 'password' ) or die(mysql_error());
mysql_select_db ('database', $link) or die(mysql_error());
$result = mysql_query ($sql) or die(mysql_error());
while($row = mysql_fetch_array($result))
{
$array2 = array ( 'Husband' => $row['husband'],
'Wife' => $row['wife'] );
print_r ($array2);
}
This will produce output (B) like:
- Array ( [Husband] => bob [Wife] => ann ) Array ( [Husband] => fred [Wife] => donna ) Array ( [Husband] => john [Wife] => mary )
My question is how do I make a php array of the mysql_fetch_array while loop, so that output B looks like output A. I want to be able to continue to access the data in my class file in the manner $array[$col]['Wife']
, but from the MySQL database.