简体   繁体   English

PHP从MySQL导出到CSV

[英]PHP export from MySQL into CSV

I need to export data from MySQL to CSV but from specific ID. 我需要将数据从MySQL导出到CSV,但要从特定ID导出。 Table contains ID, ParentID and Name and here is code which exports all records: 该表包含ID,ParentID和Name,这是导出所有记录的代码:

    <?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "databasename";
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 
    $index = array();
    $num = 0;
    $sql = "SELECT NAME, ID, PARENTID from tablename";
    $result = $conn->query($sql);

    while($row = $result->fetch_array(MYSQLI_ASSOC)){
      $rows[] = $row;
      $index[$row['ID']] = $row;  
    }

    $output = fopen("php://output",'w') or die("Can't open php://output");
    header("Content-Type:application/csv"); 
    fputcsv($output, array('ID','NAME','PARENTID'));

    // build the tree
    foreach($index as $id => &$row){
      if ($id === 0) continue;
      $parent = $row['PARENTID'];
      $index[$parent][] = &$row;
      fputcsv($output, $row);
    }
    fclose($output) or die("Can't close php://output");
    unset($row);

    // start from this ID
    $index = $index[2];

    /* free result set */
    $result->close();
    /* close connection */
    $conn->close();

If I have this in my table ( this is table declaration ): 如果我的表中有这个( 这是表声明 ):

ID  PARENT    NAME
1     0       John Doe
2     1       Sally Smith
3     2       Mike Jones
4     3       Jason Williams
5     4       Sara Johnson
6     1       Dave Wilson
7     2       Amy Martin

How to export all children's for member with parentID = 2 (recursive)? 如何导出parentID = 2(递归)的成员的所有子代? With other words, I need output like this: 换句话说,我需要这样的输出:

  ID  PARENT    NAME
  3   2         Mike Jones
  4   3         Jason Williams
  5   4         Sara Johnson
  7   2         Amy Martin

That means, exported need to be 2 members with ParentID = 2 but member Mike Jones have child (Jason Williams) which also need to be exported. 这意味着,导出的必须是ParentID = 2的2个成员,但是成员Mike Jones的孩子(Jason Williams)也需要导出。 Mike Williams have also one child (Sara Johnson) which also need to be exported. 迈克·威廉姆斯还有一个孩子(萨拉·约翰逊),也需要出口。 With other words, function need to find all members with ParentID = 2 but also they childrens and export them all. 换句话说,函数需要查找ParentID = 2的所有成员,但也要找到所有成员并将其全部导出。 Thank you for your help. 谢谢您的帮助。

To find all rows with ID=2 (ie Mike and Amy), just modify your MYSQL statement to include a WHERE clause: 要查找ID = 2的所有行(即Mike和Amy),只需修改您的MYSQL语句以包含WHERE子句:

    $sql = 'SELECT NAME, ID, PARENTID from tablename WHERE ID="2"';

To find all rows with ID=2, or rows where the parent corresponds to ID=2 (ie Mike, Jason and Amy), you can use: 要查找所有ID = 2的行,或查找父级对应于ID = 2的行(即Mike,Jason和Amy),可以使用:

    $sql = 'SELECT t1.NAME FROM test2 AS t1 JOIN test2 AS t2 ON t1.PARENTID = t2.ID WHERE t1.PARENTID = "2" OR t2.PARENTID = "2"';

The JOIN does the 'recursive' bit, finding the IDs for the values in the PARENTID column, so that you can query each line on both the ID of the row itself, and the PARENTID of the row corresponding to the PARENTID. JOIN执行“递归”位,在PARENTID列中查找值的ID,以便您可以查询行本身的ID以及与PARENTID对应的行的PARENTID的每一行。

For one additional level of recursivity (ie to find Mike, Jason, Amy and Sara), use: 要获得更高级别的递归性(即查找Mike,Jason,Amy和Sara),请使用:

    $sql = 'SELECT t1.NAME FROM test2 AS t1 JOIN test2 AS t2 ON t1.PARENTID = t2.ID JOIN test2 AS t3 ON t1.PARENTID = t3.ID WHERE t1.PARENTID = '2' OR t2.PARENTID = '2' OR t3.PARENTID = '3'';

In this solution, you will need to add an extra join for each level of recursivity. 在此解决方案中,您将需要为递归的每个级别添加一个额外的联接。

$sql = "SELECT NAME, ID, PARENTID from tablename WHERE ID=2";
 $sql = "SELECT NAME, ID, PARENTID from tablename Where ID='2'";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM