从 While 循环填充 PHP 数组 [英] Populate PHP Array from While Loop
本文介绍了从 While 循环填充 PHP 数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我从 MySQL 数据库中获取数据并使用 while 循环遍历数据,我将如何将每个数据添加到数组中?
If I am fetching data from a MySQL database and using a while loop to iterate through the data how would I add each one to array?
$result = mysql_query("SELECT * FROM `Departments`");
while($row = mysql_fetch_assoc($result))
{
}
推荐答案
在使用 while
循环进行迭代时构建一个数组.
Build an array up as you iterate with the while
loop.
$result = mysql_query("SELECT * FROM `Departments`");
$results = array();
while($row = mysql_fetch_assoc($result))
{
$results[] = $row;
}
Alternatively, if you used PDO, you could do this automatically.
这篇关于从 While 循环填充 PHP 数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文