【发布时间】:2010-06-24 06:41:08
【问题描述】:
我需要获取用户表中的所有用户,然后将结果作为数组获取并遍历它并为每个用户创建一个 XML 文件。
我该怎么做呢?
我猜查询类似于SELECT * FROM users,但我不确定如何将所有结果作为一个数组获取,然后如何逐个遍历它们并创建 XML。
提前谢谢!
【问题讨论】:
我需要获取用户表中的所有用户,然后将结果作为数组获取并遍历它并为每个用户创建一个 XML 文件。
我该怎么做呢?
我猜查询类似于SELECT * FROM users,但我不确定如何将所有结果作为一个数组获取,然后如何逐个遍历它们并创建 XML。
提前谢谢!
【问题讨论】:
这是一个例子-//我还没有运行/测试它
$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
}
}
【讨论】:
手册页有一个示例供您参考:http://www.php.net/manual/en/function.mysql-query.php
您只需将$row 添加到数组中,而不是将其元素打印出来,$xmlarr[] = $row; 在while 循环内。
就是这样
【讨论】: