查询数据库以获取数组

时间:2010-06-24 06:41:09

标签: php mysql arrays

我需要获取用户表中的所有用户,然后将结果作为数组获取并遍历它并为每个用户创建XML文件。

我将如何做到这一点?

我猜这个查询会像SELECT * FROM users那样但我不确定如何将所有结果作为数组获取,然后如何逐个遍历它们并创建XML。< / p>

提前Thanx!

2 个答案:

答案 0 :(得分:0)

手册页有一个例子:http://www.php.net/manual/en/function.mysql-query.php

您只需将$row添加到数组,而不是将$xmlarr[] = $row;循环内的while打印出来。

这就是全部

答案 1 :(得分:0)

这是一个例子 - //我没有运行/测试它

$result =  mysql_query("select * from user");

if(mysql_num_rows($result)>0){
  while($row = mysql_fetch_array($result)){
    $xml_nodes[] = $row;// or you can create XML file for the user of this $row here
  }
}

//this would be double time doing the same thing, if u dont use seperate function to 
// populate $xml_nodes array and return that array
if(isset($row)){
  foreach($xml_nodes as $user_node){
     //create XML file for the user of $user_node here
  }
}
相关问题