查询数据库以获取数组
问题描述:
我需要获取用户表中的所有用户,然后以数组形式获取结果并遍历它,并为每个用户创建一个XML文件。查询数据库以获取数组
我该怎么做呢?
我猜这个查询应该是类似SELECT * FROM users
的东西,但我不确定如何将所有结果作为一个数组来获得,然后如何遍历它们并创建XML。
Thanx提前!
答
这里是一个示例 - //我还没有运行/测试
$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
循环内。
这是所有