目前,我有大量的PHP文件mysql_fetch_array的PHP數組,每一個與格式數組聲明:如何使while循環
$array1 = array (array ('Husband' => 'bob',
'Wife' => 'ann'),
array ('Husband' => 'fred',
'Wife' => 'donna'),
array ('Husband' => 'john',
'Wife' => 'mary'));
這當然回報從print_r ($array1)
的輸出(A):
Array ([0] => Array (
[Husband] => bob [Wife] => ann)
[1] => Array (
[Husband] => fred [Wife] => donna)
[2] => Array ([Husband] => john [Wife] => mary))
在一個單獨的類文件,我訪問$ ARRAY1數據,使用語句等:
for ($col = 0; $col < sizeof($array1); $col++)
{
echo $array[$col]['Wife']."<br />";
}
關於大量任何事情都是數據庫的理論,我希望將多個php數組文件中的數據遷移到MySQL數據庫中,並從我的類文件中訪問MySQL數據。所以我成立了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');
然後,我從PHP訪問這個MySQL數據庫:
$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);
}
這將產生輸出(B),如:
Array ([Husband] => bob [Wife] => ann)
Array ([Husband] => fred [Wife] => donna)
Array ([Husband] => john [Wife] => mary)
我的問題是如何製作mysql_fetch_array while循環的php數組,以便輸出B看起來像輸出A.我希望能夠以$array[$col]['Wife']
的方式繼續訪問我的類文件中的數據,但是從MySQL數據庫。
請不要打電話給你的表'陣列這是不好的做法。它應該更詳細地描述您存儲的對象 – allen213