简体   繁体   中英

Export and download my php table data to csv

I'm trying to get my datbase fetched data to Excel file for downloading it in execl or csv, but I'm having problems with exporting and also No datas are fetching to the csv .Here is my code:

This is my php code

 <?php  
 $connect = new PDO('mysql:host=localhost;dbname=123', '123', 'invoice123');
 $query ="SELECT * from tbl_order";  
 $result = mysqli_query($connect, $query);  
 ?>

this is my html

<form method="post" action="export.php" align="center">  
                     <input type="submit" name="export" value="CSV Export" class="btn btn-success" />  
                </form>  
      <table id="data-table" class="table table-bordered table-striped">
        <thead>
          <tr style="background-color: cornflowerblue;">
            <th>Invoice No.</th>
            <th>Invoice Date</th>
            <th>Student Name</th>
            <th>Total Amount</th>
            <th>Total Amount</th>
            <th>Total Amount</th>
            <th>Total Amount</th>
            <th>Total Amount</th>
            <th>Total Amount</th>
            <th>Total Amount</th>

          </tr>
        </thead>
        <?php  
                     while($row = mysqli_fetch_array($result))  
                     {  
                     ?> 

              <tr>
                 <td><?php echo $row["order_id"]; ?></td>
                <td><?php echo $row["order_no"]; ?></td>
                <td><?php echo $row["order_date"]; ?></td>
                <td><?php echo $row["order_receiver_name"]; ?></td>
                <td><?php echo $row["order_total_before_tax"]; ?></td>
                <td><?php echo $row["order_total_tax1"]; ?></td>
                <td><?php echo $row["order_total_tax2"]; ?></td>
                <td><?php echo $row["order_total_tax"]; ?></td>
                <td><?php echo $row["order_total_after_tax"]; ?></td>
                <td><?php echo $row["order_datetime"]; ?></td>

              </tr>
                <?php       
                     }  
             ?> 

      </table>

    </div>
    <br>

this is my export.php page

 <?php  
      //export.php  
 if(isset($_POST["export"]))  
 {  
      $connect = new PDO('mysql:host=localhost;dbname=123', '123', 'invoice123');
      header('Content-Type: text/csv; charset=utf-8');  
      header('Content-Disposition: attachment; filename=data.csv');  
      $output = fopen("php://output", "w");  
      fputcsv($output, array('
Invoice No.', 'Invoice Date', 'Student Name', 'Total Amount'));  
      $query = "SELECT * from tbl_order";  
      $result = mysqli_query($connect, $query);  
      while($row = mysqli_fetch_assoc($result))  
      {  
           fputcsv($output, $row);  
      }  
      fclose($output);  
 }  
 ?>

I need to view these datas on my webpage and also i need to export and download it.

Here you go!

change

 $connect = new PDO('mysql:host=localhost;dbname=123', '123', 'invoice123');

to

 $connect = mysqli_connect('localhost','123','invoice123','123');

and it should work, you can't use "PDO" with mysqli_query because both are different ways to connect to the database.

You can read more about PDO and mysqli here ->

https://websitebeaver.com/php-pdo-vs-mysqli

You should also enable error reporting. because when I tested the PHP codes it returned an errror.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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