dsc56927 2011-07-19 01:44
浏览 51
已采纳

从While循环填充PHP数组

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))
{

}
  • 写回答

5条回答 默认 最新

  • dongxiaoshe0737 2011-07-19 01:45
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动