Populate PHP Array from While Loop

后端 未结 5 763
清歌不尽
清歌不尽 2020-12-12 19:41

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(\"SEL         


        
5条回答
  •  囚心锁ツ
    2020-12-12 20:22

    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.

提交回复
热议问题