【问题标题】:Query database to get back an array查询数据库以取回数组
【发布时间】:2010-06-24 06:41:08
【问题描述】:

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

我该怎么做呢?

我猜查询类似于SELECT * FROM users,但我不确定如何将所有结果作为一个数组获取,然后如何逐个遍历它们并创建 XML。

提前谢谢!

【问题讨论】:

    标签: php mysql arrays


    【解决方案1】:

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

    $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
      }
    }
    

    【讨论】:

      【解决方案2】:

      手册页有一个示例供您参考:http://www.php.net/manual/en/function.mysql-query.php

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

      就是这样

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-11-26
        • 1970-01-01
        • 2018-07-31
        • 1970-01-01
        • 2021-09-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-22
        相关资源
        最近更新 更多